LINQ到对象列表的差异 [英] LINQ to Objects List Difference

查看:41
本文介绍了LINQ到对象列表的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个EmailAddress通用列表,我想要一种简单的方法来获取List1中所有不在List2中的所有EmailAddress对象.

我正在考虑与Linq进行某种形式的左外部连接,但是我对如何设置它感到有些困惑.我也愿意寻求更好的解决方案.

更新:我应该注意到这些是我的"EmailAddress"对象的自定义数据类型列表.

解决方案

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<EmailAddress> 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;

这篇关于LINQ到对象列表的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆