LINQ to Objects List Difference

后端 未结 5 1001
有刺的猬
有刺的猬 2021-01-20 20:55

I have two EmailAddress generic lists, I wanted a simple way of just getting all the EmailAddress objects that are in List1 that aren\'t in List2.

I\'m thinking a le

5条回答
  •  被撕碎了的回忆
    2021-01-20 21:34

    Most of these answers will not work since the items in List1 and List2 may be equal in the eyes of the user, but are actually references to different instances (they are not reference equal).

    Assuming Address is a string property of EmailAddress, here's a left join solution.

    IEnumerable query = 
      from a1 in list1
      join a2 in list2 on a1.Address equals a2.Address into g
      from x in g.DefaultIfEmpty()
      where x == null
      select a1;
    

提交回复
热议问题