Quick way to check 2 lists are the same c#

c#, ienumerable, linq

Solution

I believe this is probably the cleanest and simplest solution for you.

var list1Subset = list1.Select(i => new {i.Name, i.Cat});
var list2Subset = list2.Select(i => new {i.Name, i.Cat});

bool equal = list1Subset.SequenceEqual(list2Subset);

Problem

Suppose there are these two strongly typed lists: List 1 : existingitems ID, Name, Cat 1, ABC, C 2, BCD, D 3, NNN, F List 2 : newitems ID, Name, Cat 9, ABC, C 15, BCD, D 12, NNN, F Basically, I want to check that the Name and Cat values are the same in both lists. If the two lists are identical on these two columns, return true, otherwise false. I'd tried a few variations mostly around the below but always seems to return true, even is the newitems list has a new row, which I would expect to return false. ``` newitems.Any(x1 => existingitems.All(x2 => (x1.Name== x2.Name) && (x1.Cat== x2.Cat))); ```

Original source