且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从另一个列表中删除项目

更新时间:2022-04-15 06:56:48

您可以使用 除外:

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();

请注意,Except 不会修改任何一个列表 - 它会使用结果创建一个新列表.

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