求下述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:
SelectManyhandles the nestedfromclauses (this is what enables the cross join behavior betweenpostListandauthorList).- The first parameter to
SelectManyfilters theauthorListfor each post, only keeping authors where theirIdmatches the post'sAuthorId. - 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
相关产品推荐
相关产品推荐

