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

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-07T19:53:10Z Indexed on 2010/04/07 20:13 UTC
Read the original article Hit count: 388

Filed under:
|

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'.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ