问题描述
我有两个EmailAddress通用列表,我想要一种简单的方法来获取List1中所有不在List2中的所有EmailAddress对象.
我正在考虑与Linq进行某种形式的左外部连接,但是我对如何设置它感到有些困惑.我也愿意寻求更好的解决方案.
更新:我应该注意到这些是我的"EmailAddress"对象的自定义数据类型列表.
假设地址是EmailAddress的字符串属性,这是一个左连接解决方案.
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;
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 left outer join of some sort with Linq but I'm a little confused on how to set that up. I'm open to a better solution as well.
Update: I should have noted these are custom data type lists of my "EmailAddress" objects.
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到对象列表的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!