Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get items that are and are not in a list

Tags:

c#

linq

I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos.

Assuming that I can compare objects of V against objects of T, I'd like to find which items are in listOfOnes but, not in listOfTwos. And vice versa.

ex:

        var listOfOnes = new List<One>
        {
            new One
            {
                name = "chris",
                type = "user"
            },
            new One
            {
                name = "foo",
                type = "group"
            },
            new One
            {
                name = "john",
                type = "user"
            },
        };

        var listOfTwos = new[]
        {
            new Two
            {
                name = "chris",
                type = "user"
            },
            new Two
            {
                name = "john",
                type = "user"
            },
            new Two
            {
                name = "the Steves",
                type = "group"
            }
        };


        var notInTwos; //= listOfOnes.FindDifferences(listOfTwos); 
        //find all objects not in listOfTwos. Should find 'foo'.

        var notInOnes; //= listOfTwos.FindDifferences(listOfOnes)
        //find all objects not in listOfOnes. Should find 'the Steves'.
like image 543
Chris Martin Avatar asked Feb 03 '26 06:02

Chris Martin


2 Answers

If you can convert one of the types to the other, you can use Except and Intersect, for example:

listOfOnes.Except(listOfTwos.Cast<One>())

Otherwise you can test for each element in the first list if it is equal to any of the elements in the second list:

var notInTwos = listOfOnes.Where(one =>
    !listOfTwos.Any(two => two.Equals(one)));

This won't be as fast though.

like image 169
Mark Byers Avatar answered Feb 04 '26 20:02

Mark Byers


Perhaps something like

public static IEnumerable<T> FindDifference<U> (
    this IEnumerable<T> a, 
    IEnumerable<U> b,
    Func<T, U> convert)
{
    IEnumerable<T> bConvertedToT = b.Select (item => convert (item));
    IEnumerable<T> aNotInB = a.Except (bConvertedToT);
    return aNotInB;
}
like image 33
johnny g Avatar answered Feb 04 '26 20:02

johnny g



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!