Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two lists - get common fields?

Tags:

c#

linq

I have two List<object>. A field in the objects is party_id.

Is there a way, using LINQ, to get a list back of only the common party_ids? So, maybe join the lists on Party ID and then return the ones with matches?

In SQL, I guess I'd do:

SELECT DISTINCT party_id FROM table1
INNER JOIN table2 on table1.party_id = table2.party_id

Thanks.

like image 713
Craig Avatar asked Sep 17 '26 07:09

Craig


1 Answers

You can try this:

table1.Select(r => r.party_id).Intersect(table2.Select(r => r.party_id))
like image 78
Sergey Kalinichenko Avatar answered Sep 19 '26 23:09

Sergey Kalinichenko