C# LINQ比较两个列表并删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14191458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:00:32  来源:igfitidea点击:

LINQ compare two lists and remove

c#linq

提问by ShaneKm

I have two lists. I want to remove any items from LIST1 that are NOT present in LIST2.

我有两个清单。我想从 LIST1 中删除 LIST2 中不存在的任何项目。

So for example:

例如:

        var list1 = new List<DownloadTask>();
        list1.Add(new DownloadTask{ OperationID = 1, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 2, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 3, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 3, MachineID = 2 });

        var list2 = new List<DownloadTask>();
        list2.Add(new DownloadTask{ OperationID = 1, MachineID = 1 });
        list2.Add(new DownloadTask{ OperationID = 3, MachineID = 2 });

After run list1should contain only items: with combination operationId = 1, machineId = 1AND OperationId = 3, MachineId =2.

运行后list1应仅包含项目:与组合operationId = 1, machineId = 1AND OperationId = 3, MachineId =2

采纳答案by Jon Skeet

Does DownloadTaskoverride Equalsand GetHashCodecorrectly? If so, all you need is:

是否DownloadTask覆盖EqualsGetHashCode正确?如果是这样,您只需要:

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

That's if you're happy to create a new list, of course. If you really want to remove them from the existing list, it's slightly harder. It would quite possibly be simplest to work out what the result should look like, then clear and re-add:

当然,前提是您乐于创建新列表。如果您真的想将它们从现有列表中删除,则稍微困难一些。确定结果应该是什么样子很可能是最简单的,然后清除并重新添加:

var newList = list1.Intersect(list2).ToList();
list1.Clear();
list1.AddRange(newList);

Of course, all of this doesrequire you to implement equality appropriately in DownloadTask- but if you haven't done so already, it sounds like it would be a good idea to do so. (Or at least implement IEqualityComparer<DownloadTask>somewhere - you can pass a comparer to Intersect.)

当然,所有这一切需要您适当地实现平等DownloadTask- 但如果您还没有这样做,那么这样做听起来是个好主意。(或者至少在IEqualityComparer<DownloadTask>某处实现- 您可以将比较器传递给Intersect.)

As a side note, I view "only keep the elements in list1which are also in list2" (i.e. intersection) as a simpler way of looking at the problem than "remove all elements from list1which aren't in list2" - the latter is basically a double negative, which is always a bit of a pain.

作为一个方面说明,我认为“只保留中的元素list1也被加list2”(即路口)为看着比这个问题更简单的方法“删除所有元素从list1哪个不在list2” -后者基本上是一个双消极的,这总是有点痛苦。

回答by ShaneKm

I think it should be:

我觉得应该是:

list1.RemoveAll(x => list2.Exists(y => y.OperationID == x.OperationID && y.MachineID == x.MachineID));

回答by Pranav1688

  var lst = (from lst1 in list1 
             where !list2.Any(
                               x => x.OperationID == lst1.OperationID && 
                                    x.MachineID == lst1.MachineID )
             select lst1).ToList();

  list1 = lst.ToList();

Please try this it should be worked.

请试试这个它应该可以工作。