You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求下述LINQ查询表达式对应的Lambda表达式等价写法

Equivalent Lambda Expression for Your LINQ Query

Hey there! Let's convert that LINQ query syntax into its equivalent Lambda expression. Here's the direct translation that matches the logic of your original query:

var e = postList.SelectMany(
    p => authorList.Where(a => p.AuthorId == a.Id),
    (p, a) => new { p, a }
);

Quick Breakdown:

  • SelectMany handles the nested from clauses (this is what enables the cross join behavior between postList and authorList).
  • The first parameter to SelectMany filters the authorList for each post, only keeping authors where their Id matches the post's AuthorId.
  • The second parameter projects the matching post-author pairs into the same anonymous type your original query uses.

If you prefer a more verbose (but functionally equivalent) version that mirrors the "filter after joining" structure of your original query, you could also write it like this:

var e = postList
    .SelectMany(p => authorList, (p, a) => new { p, a })
    .Where(x => x.p.AuthorId == x.a.Id);

Just note that the first version is more efficient because it filters the author list before combining it with each post, rather than creating a full Cartesian product first and then filtering.

内容的提问来源于stack exchange,提问作者Everton Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:07:46