windows 如何在 C#2.0 中从另一个通用列表中减去一个通用列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2266682/
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-09-15 13:56:11  来源:igfitidea点击:

How to subtract one generic list from another in C#2.0

c#windowsc#-2.0device-drivergeneric-list

提问by Fueled

First of all, it very well could be that I'm approaching my problem the wrong way, in which case I'd gladly accept alternatives.

首先,很可能我以错误的方式处理我的问题,在这种情况下,我很乐意接受替代方案。

What I'm trying to achieve is to detect which drive was created after a USB device has been connected to a computer.

我想要实现的是检测在 USB 设备连接到计算机后创建了哪个驱动器。

Here is the simplified workflow:

这是简化的工作流程:

// Get list of removable drives before user connects the USB cable
List<string> listRemovableDrivesBefore = GetRemovableDriveList();

// Tell user to connect USB cable
...

// Start listening for a connection of a USB device
...

// Loop until device is connected or time runs out
do
{
    ...
} while

// Get list of removable drives after USB device is connected
List<string> listRemovableDrivesAfter = GetRemovableDriveList();

// Find out which drive was created after USB has been connected
???

GetRemovableDriveListreturns a list of strings of the removable drive letters. My idea was to get a list of removable drives beforethe device is connected, and another list afterit is connected, and that by removing the contents of the first list from the second, I would be left with drives that were just connected (normally only one).

GetRemovableDriveList返回可移动驱动器号的字符串列表。我的想法是在设备连接之前获取可移动驱动器列表,连接后获取另一个列表,并且通过从第二个列表中删除第一个列表的内容,我将留下刚刚连接的驱动器(通常只有一个)。

But I can't find a simple way of "subtracting" one list from another. Anyone could suggest a solution, or even a better way of achieving what I'm trying to do.

但是我找不到从另一个列表中“减去”一个列表的简单方法。任何人都可以提出解决方案,甚至是实现我正在尝试做的事情的更好方法。

Note: project is targeting the .NET framework 2.0, so no LINQ possible.

注意:项目面向 .NET 框架 2.0,因此不可能使用 LINQ。

Thanks!

谢谢!

采纳答案by LukeH

For a small number of elements then a foreachloop with a Containscall should do the trick:

对于少量元素foreach,带有Contains调用的循环应该可以解决问题:

List<string> listRemovableDrivesBefore = GetRemovableDriveList();
// ...
List<string> listRemovableDrivesAfter = GetRemovableDriveList();

List<string> addedDrives = new List<string>();
foreach (string s in listRemovableDrivesAfter)
{
    if (!listRemovableDrivesBefore.Contains(s))
        addedDrives.Add(s);
}

If the collection has many elements then you could make the lookups more efficient by using a Dictionary<K,V>rather than a List<T>. (Ideally you'd use a HashSet<T>, but that's not available in version 2 of the framework.)

如果集合有很多元素,那么您可以使用 aDictionary<K,V>而不是 a来提高查找效率List<T>。(理想情况下您会使用HashSet<T>,但在框架的第 2 版中不可用。)

回答by Lee

A general way to do this would be to add all the items from the source collection to a dictionary and then remove items in the other collection:

执行此操作的一般方法是将源集合中的所有项目添加到字典中,然后删除另一个集合中的项目:

public static IEnumerable<T> Subtract<T>(IEnumerable<T> source, IEnumerable<T> other)
{
    return Subtract(source, other, EqualityComparer<T>.Default);
}

public static IEnumerable<T> Subtract<T>(IEnumerable<T> source, IEnumerable<T> other, IEqualityComparer<T> comp)
{
    Dictionary<T, object> dict = new Dictionary<T, object>(comp);
    foreach(T item in source)
    {
        dict[item] = null;
    }

    foreach(T item in other)
    {
        dict.Remove(item);
    }

    return dict.Keys;
}

回答by Andres

You can work with Linq extension method's Subtractand Insersect, same as you do with math set.

您可以使用 Linq 扩展方法的SubtractInsersect,就像使用数学集一样。

A = Original.

A = 原件。

B = After.

B = 之后。

A - (A Intersect B) = removed from original B - (A insersect B) = new

A - (A Intersect B) = 从原来的 B 中移除 - (A insect B) = 新

var intersect = A.Intersect(B);

var intersect = A.Intersect(B);

var removed = A.Substract(intersect); var new = B.Substract(intersect)

var 移除 = A.Substract(intersect); var new = B.Substract(intersect)

Hope this works for you.

希望这对你有用。