Left join on two Lists and maintain one property from the right with Linq

前端 未结 4 714
日久生厌
日久生厌 2021-02-06 01:08

I have 2 lists of the same type. The left list:

var leftList = new List();
leftList.Add(new Person {Id = 1, Name = \"John\", Chang         


        
4条回答
  •  隐瞒了意图╮
    2021-02-06 01:51

    Why don't you try a solution like this:

    var query = (from left in leftList
        join right in rightList on left.Id equals right.Id into joinedList
        from sub in joinedList.DefaultIfEmpty()
        select new Person { 
            Id = left.Id,
            Name = left.Name,
            Changed = sub == null ? left.Changed : sub.Changed }).ToList();
    

提交回复
热议问题