将 lambda 表达式组合到属性路径
我需要能够将 2 个 lambda 表达式合并为 1 个:
这将帮助我创建类型安全包含的扩展(对于 EF)。
现在您可以这样做:
context.House
.Include(x => x.Doors.Doorknobs)
我希望能够将上述语句分成不同的方法。
就像是 IncludeDoorKnobs(query, expressionFromRoot, expressionFromCurrentToChild)
然后我想 - 将组合表达式包含到查询中 - 包括额外的子项(从当前)到该查询 - 调用其他类似的方法,包括树的另一部分。
我对 Lambda 的了解显然还不够,我真的需要尽快了解它们,但现在,我必须求助于 SOF...
I'd need to be able to combine 2 lambda expressions into 1:
This would serve me to create an extension to the type-safe includes (for EF).
Now you can do:
context.House
.Include(x => x.Doors.Doorknobs)
I'd like to be able to split up the above statement into different methods.
something like
IncludeDoorKnobs(query, expressionFromRoot, expressionFromCurrentToChild)
Then I'd like to
- Include the combined expression to the query
- Include extra childs (from current) to that query
- Call other similar methods, including another part of the tree.
My knowledge of Lambda's clearly comes short, and I'd really need to get into them soon, but for now, I have to resort tho SOF...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 LINQ-to-SQL,这将是微不足道的; 您只需使用
Expression.Invoke
告诉它在给定点使用现有的子表达式(带有参数替换)。 但是,EF 不(或者我上次检查时不)支持这一点。不幸的是,组合两个表达式的过程没有这是......复杂的; 您本质上需要完全重新构建内部树,对参数等内容进行手动替换。 我确实有一些代码可以做到这一点,但它远非简单(而且它不是“手头的”)。
但我想知道:这真的值得这么复杂吗?
With LINQ-to-SQL this would be trivial; you just use
Expression.Invoke
to tell it to use an existing sub-expression (with parameter substitution) at the given point. However, EF doesn't (or didn't last time I checked) support this.Unfortunately, the process for combining two expressions without this is... complex; you essentially need to completely re-build the inner tree, doing manual substitution for things like parameters. I did have some code that did this, but it is far from simple (and it isn't "to hand").
But I wonder: is it really worth the complexity?