lambda表达式相结合,财产路径 [英] combining lambda expressions to property paths

查看:113
本文介绍了lambda表达式相结合,财产路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够2 lambda表达式组合成1:

I'd need to be able to combine 2 lambda expressions into 1:

这将有助于我创建的扩展类型安全,包括(用于EF 。)

This would serve me to create an extension to the type-safe includes (for EF).

现在你可以这样做:

context.House
.Include(x => x.Doors.Doorknobs)

我想能够在上面的语句分成不同的方法。

I'd like to be able to split up the above statement into different methods.


IncludeDoorKnobs(查询,expressionFromRoot,expressionFromCurrentToChild)

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.

我的LAMBDA的知识明显短于,和我真的需要尽快进入他们,但现在,我不得不求助于寿SOF ...

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...

推荐答案

使用LINQ到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?

这篇关于lambda表达式相结合,财产路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆