简体   繁体   English

如何从基于另一个列表的元素和条件的一个列表中删除元素?

[英]How to Remove elements from one List based another list's element and condition?

How to remove list items from list1 which mats condition with list2 items using LINQ without duplicates 如何从列表list1删除列表项,该列表项使用LINQ在条件与列表2项list2情况下不重复

I know how to do it in simple foreach way but i want the same using Linq style single line code. 我知道如何以简单的foreach方式进行操作,但我希望使用Linq样式的单行代码也能做到相同。

How to do it using Linq ? 如何使用Linq做到这一点?

Input list 输入清单

list1 = new List(){new object{id = 40},new object{id = 50},new object{id = 60}}
list2 = new List(){new object{id = 400},new object{id = 50},new object{id = 600}}

Expected Output should from list1 预期输出应来自list1

new object{id = 40},new object{id = 60}

您可以删除以下元素:

list1.RemoveAll(item => list2.Any(item2 => item.Key == item2.Key))

尝试这个:

var list = list1.RemoveAll(l => list2.Contains(l));

In other perspective, 换句话说,

var list1 = Provider.FillList<SomeType>();
var list2 = Provider.FillList<SomeType>();
list1.RemoveAll(n=> list2.Exists(o=> o.Key == n.Key));

You can also try 您也可以尝试

list1.addRange(list2)
then next line would be a simple list1.Distinct(X=>x.Key1).ToList();

OR simples would be 或简单将是

list1.Except(list2,IEqualityComparer);

You would need to implement the equals method for this to work though. 但是,您将需要实现equals方法才能使其工作。

Another way would be to implement the Union 另一种方式是实施联盟

var result = List1.Union(List2, myEqualityComparer);

which uses the same logic as the except one, there are a few ways to cheat the endresult using link, most of them inefficient and take long to compute 除了使用相同的逻辑外,还有几种方法可以使用链接来欺骗最终结果,其中大多数方法效率低下,计算时间长

I would solve this with a Join statement: 我可以用Join语句解决这个问题:

var l = list1.Join(list2, outer => outer.Key, inner => inner.Key, (inner, outer) => outer);

where outer is list1 and inner is list2. 其中outer是list1, inner是list2。 The result is a list comprising all elements with the same Key , assuming that Key is a property of the elements. 结果是一个列表,其中包含具有相同Key所有元素,并假设Key是元素的属性。

The following code worked for me: 以下代码为我工作:

var list1 = new List<a> { new a { id = 40 }, new a { id = 50 }, new a { id = 60 } }; 
var list2 = new List<a> { new a { id = 400 }, new a { id = 50 }, new a { id = 600 } }; 

var exceptedItems=list1.Select(x=>x.id).Except(list2.Select(y=>y.id)).ToList(); varexceptedItems = list1.Select(x => x.id).Except(list2.Select(y => y.id))。ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个列表中删除在另一个列表中找到的元素 - Remove elements from one list that are found in another 如何使用条件从列表中删除元素 - How to remove elements from list using a condition 使用linq使用另一个条件删除一个列表中的元素 - Use linq to remove elements in one list using a condition in another 根据条件使用另一个列表中的值更新一个列表 - Updating One List with values from another list based on a condition 我应该如何使用谓词逻辑基于列表对象在C#中从另一个列表中包含元素,从而从通用列表中删除元素? - How should I remove elements from a generic list based on the list s object's inclusion of elementfrom another list in C# using predicate logic? 如何在不创建新实例的情况下根据另一个列表从列表中删除多个元素? - How to remove multiple elements from list based on another list without creating new instance? 根据另一个Hashset元素从列表对象中选择元素的单个属性 - Selecting single attribute of elements from a list object based on another Hashset's element 根据另一个列表从两个列表中删除元素 - Remove elements from two lists based on another list 根据条件将一个列表中的值添加到另一个列表中 - add values from one list to another based on a condition 从一个列表中删除元素 <T> 在另一个地方找到 - Remove elements from one List<T> that are found in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM