开发者

Dynamically creating an Expression calling method EntityFunctions.DiffDays

I am trying to create the following Where clause expression dynamically:

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

This is the code I am using to create the expression:

var parameter = Expression.Parameter(typeof(Car), "c");
var property = Expression.Property(parameter, "Created");
var function = Expression.Call(typeof(EntityFunctions), "DiffDay开发者_StackOverflow中文版s", 
    null, property, property);
var comparison = Expression.Equal(function, Expression.Constant(null));
var result = Expression.Lamda<Func<Car, bool>>(comparison, parameter);

Result shows (it seems to be missing the "EntityFunctions.DiffDays"):

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

When I try to execute with:

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

I get the error message:

this function can only be invoked from linq to entities

Do you know what I am missing? Is it because it is only showing "DateDiff" and not "EntityFunctions.DateDiff"

Thanks. Adam


Remove the "Compile()" call on your last line, like this:

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

and defer the compile to the LinqToEntities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜