5

I have two list of type Link

Link
{
    Title;
    url;
}

I have two list(List lst1 and List lst2 of type Link Now I want those element which is not in lst1 but in lst2 How can I do that using lambda expression. I dont want to use for loop.

user3138879
  • 183
  • 2
  • 9

3 Answers3

13

For reference comparison:

list2.Except(list1);

For value comparison you can use:

list2.Where(el2 => !list1.Any(el1 => el1.Title == el2.Title && el1.url == el2.url));
Alex Sikilinda
  • 2,928
  • 18
  • 34
1

In set operations what you are looking for is

a union minus the intersect

so (list1 union list2) except (list1 intersect list2)

check out this link for linq set operations https://msdn.microsoft.com/en-us/library/bb546153.aspx

XenoPuTtSs
  • 1,254
  • 1
  • 11
  • 31
0
class CompareLists
{        
    static void Main()
    {
        // Create the IEnumerable data sources. 
        string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt");
        string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt");

        // Create the query. Note that method syntax must be used here.
        IEnumerable<string> differenceQuery =
          names1.Except(names2);

        // Execute the query.
        Console.WriteLine("The following lines are in names1.txt but not names2.txt");
        foreach (string s in differenceQuery)
            Console.WriteLine(s);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}
/* Output:
     The following lines are in names1.txt but not names2.txt
    Potra, Cristina
    Noriega, Fabricio
    Aw, Kam Foo
    Toyoshima, Tim
    Guy, Wey Yuan
    Garcia, Debra
     */

EDIT

Using Except is exactly the right way do go. If your type overrides Equals and GetHashCode, or you're only interested in reference type equality (i.e. two references are only "equal" if they refer to the exact same object), you can just use:

var list3 = list1.Except(list2).ToList();

If you need to express a custom idea of equality, e.g. by ID, you'll need to implement IEqualityComparer. For example:

public class IdComparer : IEqualityComparer<CustomObject>
{
    public int GetHashCode(CustomObject co)
    {
        if (co == null)
        {
            return 0;
        }
        return co.Id.GetHashCode();
    }

    public bool Equals(CustomObject x1, CustomObject x2)
    {
        if (object.ReferenceEquals(x1, x2))
        {
            return true;
        }
        if (object.ReferenceEquals(x1, null) ||
            object.ReferenceEquals(x2, null))
        {
            return false;
        }
        return x1.Id == x2.Id;
    }
}

Then use:

var list3 = list1.Except(list2, new IdComparer()).ToList();

EDIT: As noted in comments, this will remove any duplicate elements; if you need duplicates to be preserved, let us know... it would probably be easiest to create a set from list2 and use something like:

var list3 = list1.Where(x => !set2.Contains(x)).ToList();

Difference between two lists

Community
  • 1
  • 1
Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49