comparing two lists with LINQ

Let's say that I have these two lists of Persons. The Person object has FirstName, LastName, and Age properties.

List A

David Smith, 38 David Smith, 38 Susan Johnson, 23

List B

David Smith, 38 David Smith, 38 Susan Johnson, 23 Daniel Wallace, 55

I want to see if A is a subset of B by comparing the three properties. No, in this case I do not have a unique ID for each person.

EDIT: There can be duplicates in List A (David Smith, 38). List B should have the duplicates for it to qualify as a super set of B.

Jon Skeet
people
quotationmark

Once you've got a class which implements IEquatable<T> or IEqualityComparer<T>, it's easy to do the rest with Except and Any:

if (collectionA.Except(collectionB).Any())
{
    // There are elements in A which aren't in B
}

or

if (collectionA.Except(collectionB, equalityComparer).Any())
{
    // There are elements in A which aren't in B
}

EDIT: If there are duplicates, you'd probably want to group each collection, then check the counts:

var groupedA = collectionA.GroupBy(p => p,
                                   (Value, g) => new { Value, Count = g.Count() });
var groupedB = collectionB.GroupBy(p => p,
                                   (Value, g) => new { Value, Count = g.Count() });
var extras = from a in groupedA
             join b in groupedB on a.Value equals b.Value into match
             where !match.Any() || a.Count > match.First().Count
             select a;

// ListA has at least one entry not in B, or with more duplicates than in B
if (extras.Any())
{
}

This is pretty horrible though...

people

See more on this question at Stackoverflow