拉姆达表达式

发布于 2024-08-09 04:42:11 字数 217 浏览 3 评论 0原文

我可以用 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 技术交流群。

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

发布评论

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

评论(4

心安伴我暖 2024-08-16 04:42:11
var project = accounts.SelectMany(a => a.AccountProjects)
                      .Where(x => x.AccountProjectID == accountProjectId);

这是否真的更简单是一个品味问题。

var project = accounts.SelectMany(a => a.AccountProjects)
                      .Where(x => x.AccountProjectID == accountProjectId);

Whether this is actually simpler is a matter of taste.

暗喜 2024-08-16 04:42:11

老实说,我看起来很清楚。我认为这种情况下的 lambda 可读性可能较差,即像下面布兰登发布的那样。

(从 Brandon 的帖子中窃取)

var project = accounts.Select(a => a.AccountProjects)
                      .Where(x => x.AccountProjectID == accountProjectId);

就可读性而言,我认为几个循环比 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)

var project = accounts.Select(a => a.AccountProjects)
                      .Where(x => x.AccountProjectID == accountProjectId);

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.

望笑 2024-08-16 04:42:11

我同意埃德·斯旺格伦的观点。这看起来足够简洁和可读。

实际上你的问题的答案取决于三件事:

  1. 你想要实现什么——更好的可读性?更好的性能?等。
  2. “帐户”的类型
  3. 将如何使用结果集合。

如果您想要更好的性能,并且如果“帐户”是一个列表,并且生成的集合将被迭代或传递给另一个方法,以便在这些代码行之后尽快迭代,我会这样做:

List<Account> filteredAccounts = new List<Account>();
accounts.ForEach(a => { if (a.AccountProjectID == accountProjectId) filteredAccounts.Add(a); });

当然它的可读性较差你的 LINQ 语句,但我会使用这两行而不是 account.Select.......

当然它对性能进行了更好的优化,我相信这始终很重要。

I agree with Ed Swangren. This looks concise and readable enough.

Actually the answer to your question depends on 3 things:

  1. What you want to achieve - better readability? better performance? etc.
  2. The type of 'accounts'
  3. How the resulting collection is going to be used.

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:

List<Account> filteredAccounts = new List<Account>();
accounts.ForEach(a => { if (a.AccountProjectID == accountProjectId) filteredAccounts.Add(a); });

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.

塔塔猫 2024-08-16 04:42:11
accounts
    .SelectMany (
        a => AccountProjects, 
        (a, ct) => 
        new  
        {
            a = a, 
            ap = ap
        }
    )
    .Where (t => (t.ap.AccountProjectID == t.a.accountProjectId))
    .Select (t => t.ap)
accounts
    .SelectMany (
        a => AccountProjects, 
        (a, ct) => 
        new  
        {
            a = a, 
            ap = ap
        }
    )
    .Where (t => (t.ap.AccountProjectID == t.a.accountProjectId))
    .Select (t => t.ap)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文