Daniel Valle
Daniel Valle

Reputation: 55

How do I remove the same row from a multidime list

I have two sets of data, and I put them in a multidime list as so.

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

Structure of data:

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

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

What can I do to remove {"John Doe", 21, "USA"} from both lists?

EDIT:

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++;
    }
}

The result variable is a union of two LINQ sets. The Result and Result2 are DataRows from the dtSource and dtTarget DataTable parameters.

Upvotes: 1

Views: 97

Answers (3)

sangeethkumarp
sangeethkumarp

Reputation: 88

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)
}

Upvotes: 2

Max
Max

Reputation: 1090

At first, you should find intersection elements in lists by IEnumereble.Where

var intersection= list1.Where(a => list2.Contains(a)).ToList();

Better @AviTurner advised directly to use Enumerable.Intersect

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

Then remove from both lists intersection

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

Full code:

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

Upvotes: 0

Mars Gao
Mars Gao

Reputation: 69

You can try

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

The following link might help you

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

Upvotes: 0

Related Questions