David Archer David Archer - 23 days ago 24
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:

foo[a].Intersect(bar[a]);


How would I do this?

Thanks for any help :)

Answer

Maybe

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

BTW property a should be public.

Comments