David Archer David Archer - 1 year ago 213
C# Question

C# Linq intersect/except with one part of object

I've had a look around and can't find anything to help out here. I've got a class:

class ThisClass
private string a {get; set;}
private string b {get; set;}

I would like to use the Intersect and Except methods of Linq, i.e.:

private List<ThisClass> foo = new List<ThisClass>();
private List<ThisClass> bar = new List<ThisClass>();

Then I fill the two lists seperately. I'd like to do, for example (and I know this isn't right, just pseudocode), the following:


How would I do this?

Thanks for any help :)

Answer Source


// returns list of intersecting property 'a' values
foo.Select(f => f.a).Intersect(bar.Select(b => b.a));

BTW property a should be public.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download