代码之家  ›  专栏  ›  技术社区  ›  Aducci

动态创建调用方法EntityFunctions.DiffDays的表达式

  •  2
  • Aducci  · 技术社区  · 14 年前

    我正在尝试动态创建以下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()
    

    我收到错误消息:

    只能从调用此函数 实体的Linq

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

    谢谢。亚当

    1 回复  |  直到 12 年前
        1
  •  2
  •   Sleiman Zublidi    14 年前

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

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

    并将编译推迟到linqtoEntities。