LINQ - 查找在一个列表中的所有项目不在另一个列表 [英] LINQ - Find all items in one list that aren't in another list

查看:941
本文介绍了LINQ - 查找在一个列表中的所有项目不在另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能和LINQ查询(或完成同样的事情,任何其他有效的方法)。有人能告诉我如何,我可以选择一个列表中的所有项目没有被美元的另一个列表P $ psent?

基本上,我有我通过匹配两个表之间的项目形成的列表。我需要找到在第一个列表中匹配,未发现对所有的项目。有人能填补下面,将实现这一目标的查询第二LINQ查询星星呢?如果我使用TSQL,我会做 SELECT * NOT IN(),但我不认为LINQ允许。

  //创建一些样本名单。
名单< IdentifierLookupData> List1中=新的名单,其中,IdentifierLookupData> { /*在里面 */ };
名单< IdentifierLookupData> list2中=新的名单,其中,IdentifierLookupData> { /*在里面 */ };

//查找List1中和list2中那场比赛的所有项目,并将它们存储在joinItems。
VAR joinItems =(从D1 List1中
    加入D2在list2中的D1等于D2
    选择D1).ToList< IdentiferLookupData>();

//查找List1中的所有项目没有joinItems。
VAR deletedItems =(从D1 List1中
     ***选择joinItems列表中没有找到的所有项目。***
 

解决方案

尝试使用。除扩展方法的 (文档)

  VAR的结果= list1.Except(list2中);
 

会给你的List1 所有项目不在 list2中

I'm stuck with a LINQ query (or any other efficient means of accomplishing the same thing). Can someone show me how I can select all the items in one list that are not present in another list?

Basically, I have a list I formed by matching items between two other lists. I need to find all the items in the first list that matches weren't found for. Can someone fill in the stars in the second LINQ query below with the query that would achieve this goal? If I were using TSQL, I would do SELECT * NOT IN (), but I don't think LINQ allows that.

//Create some sample lists.
List<IdentifierLookupData> list1 = new List<IdentifierLookupData> { /*Init */ };
List<IdentifierLookupData> list2 = new List<IdentifierLookupData> { /*Init */ };

//Find all items in list1 and list2 that match and store them in joinItems.
var joinItems = (from d1 in list1
    join d2 in list2 on d1 equals d2
    select d1).ToList<IdentiferLookupData>();

//Find all items in list1 not in joinItems.
var deletedItems = (from d1 in list1
     ***select all items not found in joinItems list.***

解决方案

Try using .Except extension method (docs):

var result = list1.Except(list2);

will give you all items in list1 that are not in list2.

这篇关于LINQ - 查找在一个列表中的所有项目不在另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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