LINQ - Full Outer Join

后端 未结 16 1586
既然无缘
既然无缘 2020-11-21 22:45

I have a list of people\'s ID and their first name, and a list of people\'s ID and their surname. Some people don\'t have a first name and some don\'t have a surname; I\'d l

16条回答
  •  旧时难觅i
    2020-11-21 23:15

    As you've found, Linq doesn't have an "outer join" construct. The closest you can get is a left outer join using the query you stated. To this, you can add any elements of the lastname list that aren't represented in the join:

    outerJoin = outerJoin.Concat(lastNames.Select(l=>new
                                {
                                    id = l.ID,
                                    firstname = String.Empty,
                                    surname = l.Name
                                }).Where(l=>!outerJoin.Any(o=>o.id == l.id)));
    

提交回复
热议问题