LINQ 表达式中的函数应用
我们正在尝试找到解决实体框架不支持非标量实体问题的解决方法。我们正在使用特定的等式,因此我们尝试构建一个表达式,对于给定的输入和该输入的函数检查该等式是否成立。
private static Expression<Func<TElement, bool>> UserEquals<TElement>(User user, Func<TElement, User> select)
{
var userequals = (Expression<Func<User, Boolean>>) (u => u.Source == user.Source && u.UserName == user.UserName);
//return an Expression that receives an TElement, applies |select| and then passes that result to then `userequals` expression
// and uses it's result as return value.
}
我怀疑它涉及创建一个接收参数的新表达式,但我无法弄清楚如何将 select
函数应用于该输入,然后将其结果传递给 userequals表达式。
预期用法类似于:
Context.Foo.Where(UserEquals(user, (f => f.User)).Single(f => f.Id == id);
而不是:
Context.Foo.Single(f => f.Id == id && f.User.Source == user.Source && f.User.UserName == user.UserName);
理想情况下,我们希望编写如下内容:
Context.Foo.Single(f => f.Id == id && f.User.Equals(user))
// or
Context.Foo.Single(f => f.Id == id && f.User == user)
We are trying to find a workaround for the issue that the Entity Framework doesn't support non-scalar entities. We are using a particular equality so we try to build an expression that for a given input and a function from that input checks whether that equality holds.
private static Expression<Func<TElement, bool>> UserEquals<TElement>(User user, Func<TElement, User> select)
{
var userequals = (Expression<Func<User, Boolean>>) (u => u.Source == user.Source && u.UserName == user.UserName);
//return an Expression that receives an TElement, applies |select| and then passes that result to then `userequals` expression
// and uses it's result as return value.
}
I suspect it involves creating a new expression that receives a parameter, but I cannot figure out how to apply the select
function to that input and then pass the result of that on to the userequals
expression.
The intended usage is something like:
Context.Foo.Where(UserEquals(user, (f => f.User)).Single(f => f.Id == id);
Instead of:
Context.Foo.Single(f => f.Id == id && f.User.Source == user.Source && f.User.UserName == user.UserName);
Ideally we would want to write something like:
Context.Foo.Single(f => f.Id == id && f.User.Equals(user))
// or
Context.Foo.Single(f => f.Id == id && f.User == user)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,如果我理解正确,您需要这样做:
这将允许您编写:
如果您没有用于访问 Foo 类型的 User 属性的基类,则每种类型都需要一个这样的扩展,但是,这是你可以很容易地编写 gen 代码的东西。我认为没有必要重写表达式树。
So, if I'm understanding you correctly you want to do this:
Which would allow you to write:
If you don't have a base class for accessing the User property of the type Foo you need one such extension for each type, however, that's something which you could quite easily code gen. I don't believe expression tree rewriting will be necessary.
您是否有机会寻找 调用表达式?
Are you by any chance looking for InvokeExpression?
这行得通吗?
目前,您的类型不匹配,因为 select 未在任何地方调用,因此您没有
TElement
作为输入,但已经是User
。希望这有帮助...
Could this work ?
Currently you have a type mismatch since select is invoked nowhere, and thus you have not a
TElement
as input but already anUser
.Hope this helps...
该库解决了该问题: http://nuget.org/List/Packages/Microsoft.Linq .翻译
在这里阅读更多相关信息:http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider
我在生产中严重依赖于此。
This library solves the problem: http://nuget.org/List/Packages/Microsoft.Linq.Translations
Read more about it here: http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider
I'm relying heavily on this in production.