GroupJoin 與 Join 一樣,是用來組合多個資料來源的 LINQ 方法。但是,GroupJoin 產生的結果結構與 Join 不同。
Join:
<code>from p in Parent join c in Child on p.Id equals c.Id select new { p.Value, c.ChildValue }</code>
GroupJoin:
<code>from p in Parent join c in Child on p.Id equals c.Id into g select new { Parent = p, Children = g }</code>
Join:
<code>Parent.Join(Child, p => p.Id, c => c.Id, (p, c) => new { p.Value, c.ChildValue })</code>
GroupJoin:
<code>Parent.GroupJoin(Child, p => p.Id, c => c.Id, (p, c) => new { Parent = p, Children = c })</code>
<code>from p in Parent join c in Child on p.Id equals c.Id into g from c in g.DefaultIfEmpty() select new { Parent = p.Value, Child = c?.ChildValue }</code>
<code>from id in ids join p in parents on id equals p.Id select p</code>
以上是LINQ 的 Join 和 GroupJoin 方法有什麼不同?的詳細內容。更多資訊請關注PHP中文網其他相關文章!