【问题标题】:Dynamically creating an Expression calling method EntityFunctions.DiffDays动态创建 Expression 调用方法 EntityFunctions.DiffDays
【发布时间】:2010-10-19 15:10:02
【问题描述】:

我正在尝试动态创建以下 Where 子句表达式:

context.Cars.
Where(c => EntityFunctions.DiffDays(c.Created, c.Created) == null).
ToList()

这是我用来创建表达式的代码:

var parameter = Expression.Parameter(typeof(Car), "c");
var property = Expression.Property(parameter, "Created");
var function = Expression.Call(typeof(EntityFunctions), "DiffDays", 
    null, property, property);
var comparison = Expression.Equal(function, Expression.Constant(null));
var result = Expression.Lamda<Func<Car, bool>>(comparison, parameter);

结果显示(似乎缺少“EntityFunctions.DiffDays”):

{c => (DiffDays(c.Created, c.Created) == null)}

当我尝试执行时:

context.Cars.Where(result.Compile()).ToList()

我收到错误消息:

这个函数只能从 链接到实体

你知道我错过了什么吗?是不是因为它只显示“DateDiff”而不是“EntityFunctions.DateDiff”

谢谢。亚当

【问题讨论】:

    标签: c# linq-to-entities lambda dynamic predicate


    【解决方案1】:

    删除最后一行的“Compile()”调用,如下所示:

    context.Cars.Where(result).ToList();
    

    并将编译推迟到 LinqToEntities。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-08
      • 2013-06-18
      • 2011-07-02
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多