C# 删除单个数据集中相对于C中另一个数据集中的重复项#

C# 删除单个数据集中相对于C中另一个数据集中的重复项#,c#,blueprism,C#,Blueprism,我是C#的新手。 试图删除Collection1中的重复项,但无效。CollectionIn中没有删除重复项 只是澄清一下,collectionIn有[A,B,C,D],Collection2有[A,B,C] 所以我想删除collectionIn中的值(A、B、C) for (int i = 0; i < CollectionIn.Rows.Count; i++) { string value1 = CollectionIn.Rows[i].ItemArray[0].ToStrin

我是C#的新手。 试图删除Collection1中的重复项,但无效。CollectionIn中没有删除重复项

只是澄清一下,collectionIn有[A,B,C,D],Collection2有[A,B,C]

所以我想删除collectionIn中的值(A、B、C)

for (int i = 0; i < CollectionIn.Rows.Count; i++) {
    string value1 = CollectionIn.Rows[i].ItemArray[0].ToString().ToLower(); 

    for (int i2 = 0; i2 < CollectionIn2.Rows.Count; i2++) {
        string value2 = CollectionIn2.Rows[i2].ItemArray[0].ToString().ToLower(); 

        if (value1 == value2) {
            //Remove value1 when value1 == value2
            CollectionIn.Rows[i].Delete(); <--- Trying to delete when there is duplicate in both collections

            CollectionIn.AcceptChanges();
        }
    }
    //CollectionOut.Rows.Add(value1);
}
for(int i=0;iCollectionIn.Rows[i].Delete();可以使用运算符删除重复项

要从IList之类的文件中删除重复项,可以执行以下操作:

yourList.RemoveAll( yourList.Except( yourList.Distinct() ) );

可以使用运算符删除重复项

要从IList之类的文件中删除重复项,可以执行以下操作:

yourList.RemoveAll( yourList.Except( yourList.Distinct() ) );
foreach(CollectionIn.Rows.Cast()中的变量行)
.Where(x=>collection2.Rows.Cast()
.Any(y=>y[0]。ToString().ToLower()==x[0]。ToString().ToLower())
{
行。删除();
}
CollectionIn.AcceptChanges();
虽然不是最好的性能,但它工作正常且易于阅读

此外,由于在对集合进行迭代时修改了集合,您的代码中也有一个bug。

foreach(CollectionIn.Rows.Cast()中的var row)
.Where(x=>collection2.Rows.Cast()
.Any(y=>y[0]。ToString().ToLower()==x[0]。ToString().ToLower())
{
行。删除();
}
CollectionIn.AcceptChanges();
虽然不是最好的性能,但它工作正常且易于阅读


此外,由于在对集合进行迭代时修改了集合,您的代码中也有一个bug。

它可以工作并且易于理解

List<string> List1 = new List<string> { "A", "B", "C", "D" };
List<string> List2 = new List<string> { "A", "B", "C" };
List<string> ListTemp = new List<string>();

foreach (string str1 in List1)
{
     foreach (string str2 in List2)
     {
          if (str1 == str2)
          {
               ListTemp.Add(str1);
          }
     }
 }            

foreach (string temp in ListTemp)
{
     List1.Remove(temp);
}
List List1=新列表{“A”、“B”、“C”、“D”};
List List2=新列表{“A”、“B”、“C”};
List ListTemp=新列表();
foreach(列表1中的字符串str1)
{
foreach(列表2中的字符串str2)
{
如果(str1==str2)
{
列表临时添加(str1);
}
}
}            
foreach(ListTemp中的字符串温度)
{
清单1.移除(临时);
}

它工作正常,易于理解

List<string> List1 = new List<string> { "A", "B", "C", "D" };
List<string> List2 = new List<string> { "A", "B", "C" };
List<string> ListTemp = new List<string>();

foreach (string str1 in List1)
{
     foreach (string str2 in List2)
     {
          if (str1 == str2)
          {
               ListTemp.Add(str1);
          }
     }
 }            

foreach (string temp in ListTemp)
{
     List1.Remove(temp);
}
List List1=新列表{“A”、“B”、“C”、“D”};
List List2=新列表{“A”、“B”、“C”};
List ListTemp=新列表();
foreach(列表1中的字符串str1)
{
foreach(列表2中的字符串str2)
{
如果(str1==str2)
{
列表临时添加(str1);
}
}
}            
foreach(ListTemp中的字符串温度)
{
清单1.移除(临时);
}

比较两个集合的复杂度可能为O(n2)。这很糟糕。如果您有一个初始哈希查找,您可以在这方面得到改进

var Set1 = new Dictionary<string, int>();

//Prehash all values in the set that won't be deleted from 
for (int i = 0; i < CollectionIn.Rows.Count; i++)
{
    string value1 = CollectionIn.Rows[i].ItemArray[0].ToString().ToLower();
    Set1.Add(value1, i);
}

//Loop over the other set
for (int i2 = 0; i2 < CollectionIn2.Rows.Count; i2++)
{
    string value2 = CollectionIn2.Rows[i2].ItemArray[0].ToString().ToLower();

    int foundIndex;
    if (Set1.TryGetValue(value2, out foundIndex) == false)
        continue;

    //Remove value1 when value1 == value2
    CollectionIn.Rows[foundIndex].Delete();
}
CollectionIn.AcceptChanges(); //It's probably best to save changes last as a single call
var Set1=newdictionary();
//预灰化集合中不会从中删除的所有值
对于(int i=0;i
我对CollectionIn进行了散列,然后对Collection2进行了迭代。这意味着我需要一个字典,这样我就可以使用CollectionIn索引进行删除。如果反过来,并且Collection2进行了散列,那么它只需要是一个散列集,这会更好,因为它能够处理CollectionIn集中的内部重复项,因此:

var Set2 = new HashSet<string>();

//Prehash all values in one set (ideally the larger set)
for (int i2 = 0; i2 < CollectionIn2.Rows.Count; i2++)
{
    string value2 = CollectionIn2.Rows[i2].ItemArray[0].ToString().ToLower();

    if (Set2.Contains(value2))
        continue; //Duplicate value
    else
        Set2.Add(value2);
}

//Loop over the other set
for (int i1 = 0; i1 < CollectionIn.Rows.Count; i1++)
{
    string value1 = CollectionIn.Rows[i1].ItemArray[0].ToString().ToLower();

    if (Set2.Contains(value1) == false)
        continue;

    //Remove value1 when value1 == value2
    CollectionIn.Rows[i1].Delete();
}

CollectionIn.AcceptChanges(); //It's probably best to save changes last as a single call
var Set2=newhashset();
//在一个集合中预灰化所有值(理想情况下为较大的集合)
for(int i2=0;i2
这种模式适用于许多数据集类型(包括列表、数组等)。当然,如果可以在同一数据库上为远程数据集编写SQL,那就更好了

如果您喜欢lambda函数,它应该如下所示:

var alreadyInSet2 = new HashSet<string>(CollectionIn2.Rows.Cast<DataRow>()
                    .Select(x => x[0].ToString().ToLower()));

CollectionIn.Rows.Cast<DataRow>()
                    .Where(y => alreadyInSet2.Contains(y[0].ToString().ToLower()) == false)
                    .ToList() //I think you technically need this before calling ForEach
                    .ForEach(y => y.Delete());

CollectionIn.AcceptChanges();                   
var alreadyInSet2=新的HashSet(collection2.Rows.Cast()
.Select(x=>x[0].ToString().ToLower());
CollectionIn.Rows.Cast()中的集合
.Where(y=>alreadyInSet2.Contains(y[0].ToString().ToLower())==false)
.ToList()//我认为在调用ForEach之前,您在技术上需要这个
.ForEach(y=>y.Delete());
CollectionIn.AcceptChanges();

另请参阅:-在这里,更多的时间/工作可以用于更广泛的答案和性能增强。

比较两个集合的复杂性可能达到O(n2)。这很糟糕。如果您有一个初始哈希查找,您可以在这方面得到改进

var Set1 = new Dictionary<string, int>();

//Prehash all values in the set that won't be deleted from 
for (int i = 0; i < CollectionIn.Rows.Count; i++)
{
    string value1 = CollectionIn.Rows[i].ItemArray[0].ToString().ToLower();
    Set1.Add(value1, i);
}

//Loop over the other set
for (int i2 = 0; i2 < CollectionIn2.Rows.Count; i2++)
{
    string value2 = CollectionIn2.Rows[i2].ItemArray[0].ToString().ToLower();

    int foundIndex;
    if (Set1.TryGetValue(value2, out foundIndex) == false)
        continue;

    //Remove value1 when value1 == value2
    CollectionIn.Rows[foundIndex].Delete();
}
CollectionIn.AcceptChanges(); //It's probably best to save changes last as a single call
mylist2 = mylist2.Distinct().ToList();
mylist1.RemoveAll(item => mylist2.Contains(item));
var Set1=newdictionary();
//预灰化集合中不会从中删除的所有值
对于(int i=0;i