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

发布于 2024-09-27 19:00:27 字数 929 浏览 0 评论 0原文

我正在尝试动态创建以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2024-10-04 19:00:27

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

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

并将编译推迟到 LinqToEntities。

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

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

and defer the compile to the LinqToEntities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文