动态创建一个表达式调用方法EntityFunctions.DiffDays
我正在尝试动态创建以下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”
谢谢。亚当
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), "DiffDays",
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除最后一行上的“Compile()”调用,如下所示:
并将编译推迟到 LinqToEntities。
Remove the "Compile()" call on your last line, like this:
and defer the compile to the LinqToEntities.