我是C#的新手。 尝试删除CollectionIn1中的重复项,但不起作用。在CollectionIn中不会删除任何重复项。
请澄清一下,collectionIn具有[A,B,C,D],collectionIn2具有[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);
}
我对此链接做了一些更改 http://www.rpaforum.net/threads/how-to-compare-two-excel-sheet-using-c-code-in-blueprism.897/
答案 0 :(得分:2)
比较两个集合可能具有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
我散列 CollectionIn,然后迭代 CollectionIn2。这意味着我需要一个字典,以便可以使用CollectionIn索引进行删除。如果将其反转,并且对CollectionIn2进行哈希处理,则只需将其作为哈希集,这样会更好,因为它能够处理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
此模式将适用于许多数据集类型(包括列表,数组等)。当然,如果可以在同一数据库上为远程数据集编写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();
另请参阅:With two very large lists/collections - how to detect and/or remove duplicates efficiently-在这里,更多的时间/工作可以用于更广泛的答案和性能增强。
答案 1 :(得分:1)
您可以使用Distinct运算符删除重复项。
要从类似IList <>的内容中删除重复项,可以执行以下操作:
yourList.RemoveAll( yourList.Except( yourList.Distinct() ) );
答案 2 :(得分:1)
foreach(var row in CollectionIn.Rows.Cast<DataRow>()
.Where(x => CollectionIn2.Rows.Cast<DataRow>()
.Any(y => y[0].ToString().ToLower() == x[0].ToString().ToLower())))
{
row.Delete();
}
CollectionIn.AcceptChanges();
性能不是最好的,但是它可以工作并且易于阅读。
此外,由于在迭代集合时修改了集合,因此您的代码中也存在错误。
答案 3 :(得分:0)
它有效且易于理解。
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);
}
答案 4 :(得分:0)
mylist2 = mylist2.Distinct().ToList();
mylist1.RemoveAll(item => mylist2.Contains(item));