2008-11-26 52 views
3

我有Enitity类型,主键的名称和主Id的Guid。我想在LinqToSql中获取这样的Id的元素。生成带反射信息的lambda

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

我想我需要生成表达自己,但我不知道我怎么:(

回答

0

期待,并通过编译器产生的代码搜索,在反射之后,我发现这个创建拉姆达的。

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:\项目\自己\ Yabeda \源\干线\ MED \ Yabeda.Med.Mvc \ DATA \ Oper.cs 48 20 Yabeda.Med.Mvc

找不到这个错误是什么意思!