This question already has an answer here:
这个问题在这里已有答案:
- Implement “not in” (aka “not exists”) logic in LINQ 1 answer
在LINQ 1答案中实现“not in”(又名“not exists”)逻辑
I have two data tables and need to get a list of rows in one that are not in the other. For example, the tables could look like this:
我有两个数据表,需要获取一个不在另一个中的行列表。例如,表格可能如下所示:
Table1:
FN LN EMAIL POBox CustID
John Doe johndoe@here.com 123 9876
Jane Doe janedoe@here.com 234 9888
Some Guy someguy@here.com 877 8888
Table2:
FN LN EMAIL POBox
John Doe johndoe@here.com 123
Some Guy someguy@here.com 444
I need to get back, from Table1, the row containing Jane Doe (in Table1 but not in Table2) and row containing Some Guy (POBox in Table1 not same as in Table2) using Linq. I know I can loop through the rows of table and spit out the row if I cannot find it in Table2 but don't know how to do it using Linq. Comparison needs to be made using all common fields (FN, LN, EMAIL and POBox).
我需要从Table1返回使用Linq的包含Jane Doe的行(在Table1中但在Table2中没有)和包含Some Guy(Table1中的POBox与Table2中的POBox不同)的行。我知道如果我在Table2中找不到它但是不知道如何使用Linq,我可以循环遍历表行并吐出行。需要使用所有常见字段(FN,LN,EMAIL和POBox)进行比较。
1 个解决方案
#1
There are multiple ways of doing it, one would be like this.
有多种方法可以做到这一点,其中一种方式就是这样。
table1.Where(t1=>!table2.Any(t2=>t1.Email == t2.Email))
#1
There are multiple ways of doing it, one would be like this.
有多种方法可以做到这一点,其中一种方式就是这样。
table1.Where(t1=>!table2.Any(t2=>t1.Email == t2.Email))