【问题标题】:Generate lambda with Reflection Info使用反射信息生成 lambda
【发布时间】:2008-11-26 17:53:00
【问题描述】:

我有实体类型、主键名称和主 ID 的 Guid。我想在 LinqToSql 中获取此类 Id 的元素。

model.GetTable<T>().Where(t => here equality  );

我想我需要自己生成那个表达式,但我不知道如何:(

【问题讨论】:

    标签: linq-to-sql lambda


    【解决方案1】:
    【解决方案2】:

    我期待,在搜索编译器生成的代码后,在 Reflector 中我发现了这个 lambda 的创建。

        public static T GetById(Guid id)
        {
            Type entType = typeof(T);
    
            if (!CheckTable(entType)) {
                throw new TypeLoadException(string.Format(
                    "{0} is not Table Entity, has no attribute Table", entType.FullName));
            }
    
            string property = GetPrimaryKeyName(entType).Name;
    
            ParameterExpression cs;
            var lambda = Expression.Lambda<Func<Personal, bool>>(
                    Expression.Equal(
                            Expression.Property(
                                    cs = Expression.Parameter(typeof(T), "p"), 
                                    entType.GetProperty(property).GetGetMethod()
                            ), 
                            Expression.Constant(id), 
                            false, 
                            typeof(Guid).GetMethod("Equals")
                    ), new ParameterExpression[] { cs }
            );
    
            return Connection.Model.GetTable<T>().Single(lambda);
        }
    

    我需要的东西,但我有编译器异常:

    错误 5 方法的类型参数 'System.Linq.Enumerable.Single(System.Collections.Generic.IEnumerable, System.Func)' 不能 从用法推断。尝试 指定类型参数 明确地。 D:\Projects\Own\Yabeda\Source\trunk\med\Yabeda.Med.Mvc\Data\Oper.cs 48 20 Yabeda.Med.Mvc

    找不到此错误的含义!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多