C# 删除不同的项目
如何从c#中的另一个列表中删除列表中的不同项 不完全是你想要的,但是通过复制你想要保留的项目来创建一个新的列表要比编辑原始列表容易得多 如果要控制列表项的“相等”成分,请调用接受C# 删除不同的项目,c#,list,C#,List,如何从c#中的另一个列表中删除列表中的不同项 不完全是你想要的,但是通过复制你想要保留的项目来创建一个新的列表要比编辑原始列表容易得多 如果要控制列表项的“相等”成分,请调用接受IEqualityComparer实例的重载 看 不完全是你想要的,但是通过复制你想要保留的项目来创建一个新的列表要比编辑原始列表容易得多 如果要控制列表项的“相等”成分,请调用接受IEqualityComparer实例的重载 请参阅。您可以使用,但类似的情况除外: var result = list2.Except
IEqualityComparer
实例的重载
看
不完全是你想要的,但是通过复制你想要保留的项目来创建一个新的列表要比编辑原始列表容易得多
如果要控制列表项的“相等”成分,请调用接受IEqualityComparer
实例的重载
请参阅。您可以使用
,但类似的情况除外:
var result = list2.Except(list1).ToList();
例如:
List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
List<int> b = new List<int>() { 1, 2, 3, 4 };
List<int> c = a.Except(b).ToList();
List a=newlist(){1,2,3,4,5};
列表b=新列表(){1,2,3,4};
列表c=a.除了(b).ToList();
其中,列表C中只有值5。您可以使用,但类似以下情况除外:
var result = list2.Except(list1).ToList();
例如:
List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
List<int> b = new List<int>() { 1, 2, 3, 4 };
List<int> c = a.Except(b).ToList();
List a=newlist(){1,2,3,4,5};
列表b=新列表(){1,2,3,4};
列表c=a.除了(b).ToList();
其中列表C中只有值5。没有使用时那么优雅,除了(我从来不知道它存在)。。。但这是可行的:
List<string> listA = new List<string>();
List<string> listB = new List<string>();
listA.Add("A");
listA.Add("B");
listA.Add("C");
listA.Add("D");
listB.Add("B");
listB.Add("D");
for (int i = listA.Count - 1; i >= 0; --i)
{
int matchingIndex = listB.LastIndexOf(listA[i]);
if (matchingIndex != -1)
listB.RemoveAt(matchingIndex);
}
List listA=新列表();
List listB=新列表();
列表A.添加(“A”);
列表a.添加(“B”);
列表A.添加(“C”);
列表a.添加(“D”);
清单B.添加(“B”);
清单B.添加(“D”);
对于(int i=listA.Count-1;i>=0;--i)
{
int matchingIndex=listB.LastIndexOf(listA[i]);
如果(匹配索引!=-1)
列表B.RemoveAt(匹配索引);
}
没有使用时那么优雅,除了(我从来不知道它存在)。。。但这是可行的:
List<string> listA = new List<string>();
List<string> listB = new List<string>();
listA.Add("A");
listA.Add("B");
listA.Add("C");
listA.Add("D");
listB.Add("B");
listB.Add("D");
for (int i = listA.Count - 1; i >= 0; --i)
{
int matchingIndex = listB.LastIndexOf(listA[i]);
if (matchingIndex != -1)
listB.RemoveAt(matchingIndex);
}
List listA=新列表();
List listB=新列表();
列表A.添加(“A”);
列表a.添加(“B”);
列表A.添加(“C”);
列表a.添加(“D”);
清单B.添加(“B”);
清单B.添加(“D”);
对于(int i=listA.Count-1;i>=0;--i)
{
int matchingIndex=listB.LastIndexOf(listA[i]);
如果(匹配索引!=-1)
列表B.RemoveAt(匹配索引);
}
什么样的列表?什么类型的项目?哪个版本的.net平台?什么类型的列表?什么类型的项目?哪个版本的.net平台?好的,我测试了Except方法,它肯定更优雅:List listC=listA.Except(listB.ToList();这将创建第三个列表,其中只包含listA中未在listB中找到的项。好的,我测试了Except方法,它肯定更优雅:list listC=listA.Except(listB.ToList();这将创建第三个列表,其中仅包含listA中未在listB中找到的项。