C# List left join

public class Test1

{

public int ID { get; set; }

public string Name { get; set; }

}

public class Test2

{

public int ID { get; set; }

public int Age { get; set; }

}

//添加集合

List<Test1> test1 = new List<Test1>();

List<Test2> test2 = new List<Test2>();

test1.Add(new Test1() { ID = 1, Name = "zbb" });

test1.Add(new Test1() { ID = 2, Name = "czf" });

test2.Add(new Test2() { ID = 1, Age = 33 });

//左连接

var result = from l in test1

join r in test2 on l.ID equals r.ID into temp

from lr in temp.DefaultIfEmpty()

select new { l.ID, l.Name, Age = temp.Select(T => T.Age).FirstOrDefault() };

// 返回结果

ID Name Age

1 zbb 33

2 czf 0