从另一个列表中删除项目

时间:2022-06-19 08:00:01

I'm trying to figure out how to traverse a generic list of items that I want to remove from another list of items.

我正试图弄清楚如何遍历要从另一个项目列表中删除的项目的通用列表。

So let's say I have this as a hypothetical example

假设我有一个假设的例子

List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();

I want to traverse list1 with a foreach and remove each item in List1 which is also contained in List2.

我想使用foreach遍历清单1,并删除清单1中的每个项,清单2中也包含这些项。

I'm not quite sure how to go about that as foreach is not index based.

我不太确定该怎么做,因为foreach不是基于索引的。

8 个解决方案

#1


259  

You can use Except:

您可以使用除了:

List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();
List<car> result = list2.Except(list1).ToList();

You probably don't even need those temporary variables:

你甚至不需要这些临时变量:

List<car> result = GetSomeOtherList().Except(GetTheList()).ToList();

Note that Except does not modify either list - it creates a new list with the result.

请注意,Except不修改任何一个列表——它会用结果创建一个新的列表。

#2


24  

You don't need an index, as the List<T> class allows you to remove items by value rather than index by using the Remove function.

您不需要索引,因为List 类允许使用remove函数按值而不是按索引删除项。

foreach(car item in list1) list2.Remove(item);

#3


21  

I would recommend using the LINQ extension methods. You can easily do it with one line of code like so:

我建议使用LINQ扩展方法。只需一行代码就可以轻松完成:

list2 = list2.Except(list1).ToList();

This is assuming of course the objects in list1 that you are removing from list2 are the same instance.

当然,这是假设从清单2中删除的清单1中的对象是相同的实例。

#4


12  

You could use LINQ, but I would go with RemoveAll method. I think that is the one that better expresses your intent.

您可以使用LINQ,但是我将使用RemoveAll方法。我认为这是更好地表达你的意图。

var integers = new List<int> { 1, 2, 3, 4, 5 };

var remove = new List<int> { 1, 3, 5 };

integers.RemoveAll(i => remove.Contains(i));

#5


8  

In my case I had two different lists, with a common identifier, kind of like a foreign key. The second solution cited by "nzrytmn":

在我的例子中,我有两个不同的列表,有一个通用标识符,有点像外键。“nzrytmn”引用的第二种解决方案:

var result =  list1.Where(p => !list2.Any(x => x.ID == p.ID && x.property1 == p.property1)).ToList();

Was the one that best fit in my situation. I needed to load a DropDownList without the records that had already been registered.

是最适合我的。我需要加载一个下拉列表,没有已经注册的记录。

Thank you !!!

谢谢! ! !

This is my code:

这是我的代码:

t1 = new T1();
t2 = new T2();

List<T1> list1 = t1.getList();
List<T2> list2 = t2.getList();

ddlT3.DataSource= list2.Where(s => !list1.Any(p => p.Id == s.ID)).ToList();
ddlT3.DataTextField = "AnyThing";
ddlT3.DataValueField = "IdAnyThing";
ddlT3.DataBind();

#6


7  

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

#7


4  

Solution 1 : You can do like this :

解决方案1:你可以这样做:

List<car> result = GetSomeOtherList().Except(GetTheList()).ToList();

But in some cases may this solution not work. if it is not work you can use my second solution .

但在某些情况下,这种解决方案可能行不通。如果不行,你可以用我的第二种方法。

Solution 2 :

解决方案2:

List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();

we pretend that list1 is your main list and list2 is your secondry list and you want to get items of list1 without items of list2.

我们假设清单1是您的主要列表,清单2是您的次要列表,您希望获得清单1中的项目,但不包括清单2中的项目。

 var result =  list1.Where(p => !list2.Any(x => x.ID == p.ID && x.property1 == p.property1)).ToList();

#8


-4  

Here ya go..

这里你去. .

    List<string> list = new List<string>() { "1", "2", "3" };
    List<string> remove = new List<string>() { "2" };

    list.ForEach(s =>
        {
            if (remove.Contains(s))
            {
                list.Remove(s);
            }
        });

#1


259  

You can use Except:

您可以使用除了:

List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();
List<car> result = list2.Except(list1).ToList();

You probably don't even need those temporary variables:

你甚至不需要这些临时变量:

List<car> result = GetSomeOtherList().Except(GetTheList()).ToList();

Note that Except does not modify either list - it creates a new list with the result.

请注意,Except不修改任何一个列表——它会用结果创建一个新的列表。

#2


24  

You don't need an index, as the List<T> class allows you to remove items by value rather than index by using the Remove function.

您不需要索引,因为List 类允许使用remove函数按值而不是按索引删除项。

foreach(car item in list1) list2.Remove(item);

#3


21  

I would recommend using the LINQ extension methods. You can easily do it with one line of code like so:

我建议使用LINQ扩展方法。只需一行代码就可以轻松完成:

list2 = list2.Except(list1).ToList();

This is assuming of course the objects in list1 that you are removing from list2 are the same instance.

当然,这是假设从清单2中删除的清单1中的对象是相同的实例。

#4


12  

You could use LINQ, but I would go with RemoveAll method. I think that is the one that better expresses your intent.

您可以使用LINQ,但是我将使用RemoveAll方法。我认为这是更好地表达你的意图。

var integers = new List<int> { 1, 2, 3, 4, 5 };

var remove = new List<int> { 1, 3, 5 };

integers.RemoveAll(i => remove.Contains(i));

#5


8  

In my case I had two different lists, with a common identifier, kind of like a foreign key. The second solution cited by "nzrytmn":

在我的例子中,我有两个不同的列表,有一个通用标识符,有点像外键。“nzrytmn”引用的第二种解决方案:

var result =  list1.Where(p => !list2.Any(x => x.ID == p.ID && x.property1 == p.property1)).ToList();

Was the one that best fit in my situation. I needed to load a DropDownList without the records that had already been registered.

是最适合我的。我需要加载一个下拉列表,没有已经注册的记录。

Thank you !!!

谢谢! ! !

This is my code:

这是我的代码:

t1 = new T1();
t2 = new T2();

List<T1> list1 = t1.getList();
List<T2> list2 = t2.getList();

ddlT3.DataSource= list2.Where(s => !list1.Any(p => p.Id == s.ID)).ToList();
ddlT3.DataTextField = "AnyThing";
ddlT3.DataValueField = "IdAnyThing";
ddlT3.DataBind();

#6


7  

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

#7


4  

Solution 1 : You can do like this :

解决方案1:你可以这样做:

List<car> result = GetSomeOtherList().Except(GetTheList()).ToList();

But in some cases may this solution not work. if it is not work you can use my second solution .

但在某些情况下,这种解决方案可能行不通。如果不行,你可以用我的第二种方法。

Solution 2 :

解决方案2:

List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();

we pretend that list1 is your main list and list2 is your secondry list and you want to get items of list1 without items of list2.

我们假设清单1是您的主要列表,清单2是您的次要列表,您希望获得清单1中的项目,但不包括清单2中的项目。

 var result =  list1.Where(p => !list2.Any(x => x.ID == p.ID && x.property1 == p.property1)).ToList();

#8


-4  

Here ya go..

这里你去. .

    List<string> list = new List<string>() { "1", "2", "3" };
    List<string> remove = new List<string>() { "2" };

    list.ForEach(s =>
        {
            if (remove.Contains(s))
            {
                list.Remove(s);
            }
        });