拉姆达表达式
我可以用 lambda 表达式简化这个语句吗?
var project = from a in accounts
from ap in a.AccountProjects
where ap.AccountProjectID == accountProjectId
select ap;
Can I simplify this statement with a lambda expression?
var project = from a in accounts
from ap in a.AccountProjects
where ap.AccountProjectID == accountProjectId
select ap;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是否真的更简单是一个品味问题。
Whether this is actually simpler is a matter of taste.
老实说,我看起来很清楚。我认为这种情况下的 lambda 可读性可能较差,即像下面布兰登发布的那样。
(从 Brandon 的帖子中窃取)
就可读性而言,我认为几个循环比 lambda 解决方案更可取,并且我认为您的解决方案比循环更可取。
Honestly, it looks pretty clear to me. I think that a lambda in this case may be less readable, i.e., something like Brandon posted below.
(Stolen from Brandon's post)
As far as readability is concerned, I think that a couple of loops is preferable to the lambda solution, and I think that your solution is preferable to the loops.
我同意埃德·斯旺格伦的观点。这看起来足够简洁和可读。
实际上你的问题的答案取决于三件事:
如果您想要更好的性能,并且如果“帐户”是一个列表,并且生成的集合将被迭代或传递给另一个方法,以便在这些代码行之后尽快迭代,我会这样做:
当然它的可读性较差你的 LINQ 语句,但我会使用这两行而不是 account.Select.......
当然它对性能进行了更好的优化,我相信这始终很重要。
I agree with Ed Swangren. This looks concise and readable enough.
Actually the answer to your question depends on 3 things:
If you want better performance, and in case 'accounts' is a List, and the resulting collection will be iterated or passed to another method for iterating soon enough after these lines of code, I would do something like that:
Surely it's less readable then your LINQ statement, but I would use these 2 lines rather than accounts.Select.......
And surely it's much better optimized for performance, which is always important I believe.