如何从多日期列表中删除相同的行

时间:2017-08-14 03:07:35

标签: c# .net list duplicates

我有两组数据,我把它们放在一个多日期列表中。

List<List<string>> list1 = new List<List<string>>();
List<List<string>> list2 = new List<List<string>>();

数据结构:

list1 = {
          ["John Doe", 21, "USA"],
          ["John Brown", 20, "Canada"]
        };

list2 = {
          ["John Doe", 21, "USA"],
          ["John Mark", 22, "Brazil"]
        };

我可以从两个名单中删除{&#34; John Doe&#34;,21,&#34; USA&#34;}?

编辑:

foreach (var item in result)
{
    if(i < Results.Count())
    {
        sourceList.Add(new List<string>());

        foreach (DataColumn dtl in dtSource.Columns)
        {                    
            sourceList[i].Add(item.left[dtl.ColumnName].ToString().Trim());
        }
        i++;
    }
    else
    {
        targetList.Add(new List<string>());

        foreach (DataColumn dtl in dtTarget.Columns)
        {                      
            targetList[j].Add(item.right[dtl.ColumnName].ToString().Trim());
        }
        j++;
    }
}

result变量是两个LINQ集的并集。 ResultResult2是来自dtSourcedtTarget DataTable参数的DataRows。

3 个答案:

答案 0 :(得分:2)

public class StringListEqComparer : IEqualityComparer<List<string>>
{
    public bool Equals(List<string> lhs, List<string> rhs)
    {
        return lhs.SequenceEqual(rhs, StringComparer.Ordinal);
    }

    public int GetHashCode(List<string> obj)
    {
        // Something I cooked up real-fast. Shouldn't miss
        // any matching items but also not run sequence equal
        // in all the comparisons. You could improve it but
        // ensure it's fast.
        // Note that, the comparison operation is that first
        // the hashcode of two objects are compare and if they
        // match, then only the Equals method is invoked with them.
        return obj.First().GetHashCode();
    }
}

void Main()
{
    StringListEqComparer listComp = new StringListEqComparer();
    var intersect = list1.Intersect(list2, listComp);
    var list1Unique = list1.Except(intersect, listComp);
    var list2Unique = list2.Except(intersect, listComp);

    // Just in case you need them both in a single set:

    // var union = list1.Union(list2, listComp);

    // var result = union.Except(intersect, listComp); // (A U B) - (A n B)
}

答案 1 :(得分:0)

你可以尝试

List<string> mylist = new List<string>(new string[] {"John Doe", 21, "USA"});
list1.Remove(mylist);
list2.Remove(mylist);

以下链接可能会对您有所帮助

https://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx

答案 2 :(得分:0)

首先,您应该在IEnumereble.Where

列表中找到交叉点元素
var intersection= list1.Where(a => list2.Contains(a)).ToList();

更好@AviTurner建议直接使用Enumerable.Intersect

var intersect = list1.Intersect(list2).ToList();

然后从两个列表交叉点中删除

list1.RemoveAll(i => intersect.Contains(i));
list2.RemoveAll(i => intersect.Contains(i));

完整代码:

var Doe = new List<string>() { "John Doe", "21", "USA" };

List<List<string>> list1 = new List<List<string>>{
    Doe,
    new List<string>(){ "John Brown", "20", "Canada" },
};

List<List<string>> list2 = new List<List<string>>{
    Doe,
    new List<string>(){ "John Mark", "22", "Brazil" },
};

var intersect = list1.Intersect(list2).ToList().ToList();

list1.RemoveAll(i => intersect.Contains(i));
list2.RemoveAll(i => intersect.Contains(i));

.netFiddle