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

发布于 2024-08-21 18:47:47 字数 931 浏览 7 评论 0 原文

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

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

以下是简化的工作流程:

// 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
???

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

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

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

谢谢!

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.

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
???

GetRemovableDriveList returns a list of strings of the removable drive letters.
My idea was to get a list of removable drives before the device is connected, and another list after it 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).

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-08-28 18:47:47

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

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;
}

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;
}
寻找我们的幸福 2024-08-28 18:47:47

对于少量元素,则使用 foreach 循环rel="nofollow noreferrer">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);
}

如果集合有很多元素,那么您可以使用 字典 而不是列表< /a>. (理想情况下,您应该使用 HashSet ,但这在框架的版本 2 中不可用。)

For a small number of elements then a foreach loop with a Contains call should do the trick:

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.)

心安伴我暖 2024-08-28 18:47:47

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

A = 原始。

B = 之后。

A -(A 与 B 相交)= 从原始内容中删除
B - (A 插入 B) = new

var intersect = A.Intersect(B);

var 删除 = A.Substract(相交);
var new = B.Substract(intersect)

希望这对您有用。

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

A = Original.

B = After.

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

var intersect = A.Intersect(B);

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

Hope this works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文