如何在C#2.0中从一个通用列表中减去另一个通用列表
首先,很可能是我处理问题的方式不对,在这种情况下,我很乐意接受其他选择 我试图实现的是检测USB设备连接到计算机后创建的驱动器 以下是简化的工作流程:如何在C#2.0中从一个通用列表中减去另一个通用列表,c#,windows,c#-2.0,device-driver,generic-list,C#,Windows,C# 2.0,Device Driver,Generic List,首先,很可能是我处理问题的方式不对,在这种情况下,我很乐意接受其他选择 我试图实现的是检测USB设备连接到计算机后创建的驱动器 以下是简化的工作流程: // Get list of removable drives before user connects the USB cable List<string> listRemovableDrivesBefore = GetRemovableDriveList(); // Tell user to connect USB cable
// 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
???
//在用户连接USB电缆之前获取可移动驱动器的列表
List listRemovableDrivesBefore=GetRemovableDriveList();
//告诉用户连接USB电缆
...
//开始侦听USB设备的连接
...
//循环,直到设备连接或时间用完
做
{
...
}当
//连接USB设备后获取可移动驱动器列表
List listRemovableDrivesAfter=GetRemovableDriveList();
//找出连接USB后创建的驱动器
???
GetRemovableDriveList
返回可移动驱动器盘符的字符串列表。
我的想法是在设备连接之前获得一个可移动驱动器列表,在设备连接之后获得另一个列表,通过从第二个列表中删除第一个列表的内容,我将只剩下刚刚连接的驱动器(通常只有一个)
但我找不到一个简单的方法从一个列表中“减去”另一个列表。任何人都可以提出一个解决方案,甚至是一个更好的方法来实现我的目标
注意:project的目标是.NET framework 2.0,因此不可能使用LINQ
谢谢 对于少量元素,则带有调用的
foreach
循环应该可以实现以下功能:
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);
}
List listRemovableDrivesBefore=GetRemovableDriveList();
// ...
List listRemovableDrivesAfter=GetRemovableDriveList();
List addedDrives=新列表();
foreach(listRemovableDrivesAfter中的字符串s)
{
如果(!listRemovableDrivesBefore.Contains))
增补地址。增补;
}
如果集合中有许多元素,则可以通过使用而不是使用来提高查找效率。(理想情况下,您可以使用,但在框架的第2版中不可用。)执行此操作的一般方法是将源集合中的所有项添加到字典中,然后删除其他集合中的项:
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;
}
公共静态IEnumerable减法(IEnumerable源,IEnumerable其他)
{
返回减法(source、other、EqualityComparer.Default);
}
公共静态IEnumerable减法(IEnumerable源、IEnumerable其他、IEqualityComparer comp)
{
Dictionary dict=新词典(comp);
foreach(源中的T项)
{
dict[项目]=空;
}
foreach(其他中的T项)
{
删除(项目);
}
返回指令键;
}
您可以使用Linq扩展方法的减法和插入法,与使用数学集的操作相同
A=原件
B=之后
A-(A相交B)=从原始数据中删除
B-(A插入B)=新
var intersect=A.intersect(B)
var移除=A.减(相交);
var new=B.减(相交)
希望这对您有用。这是一个有效的解决方案,但该方法的名称不正确。它不会返回两个序列的交集。在我看来,两个答案都是有效的,但由于我的要求,我选择了简单的方法。我选择这个答案是因为我需要一次性的东西。如果我需要在不同的地方重复执行此操作,我可能已经实现了Lee答案中的减法。我想你是说。