简体   繁体   中英

Find difference from IList<string[]> one and IList<string[]> two using Linq operation

Hi is there any build in method using Linq or lambda expression or any other approach we can get records which is present and in IList<string[]> one but not in IList<string[]> two or vise versa. I tried with linq Except keyword but its not working, it's working perfect when I am doing operation on string[] not on IList<string[]> .

You need to implement an IEqualityComparer for string[] , arrays are compared by reference by default. Even if the elements of your arrays are same, the references are different.So the Except method treats them as different.

class ArrayComparer : IEqualityComparer<string[]>
{
    public bool Equals(string[] x, string[] y)
    {
        // do your comparison here
    }

    public int GetHashCode(string[] obj)
    {
        // write your hash algorithm 
    }

}

Then you can use this comparer by passing Except method like this:

var result = list1.Except(list2, new ArrayComparer());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM