Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query help

Tags:

c#

linq

I have two collections

List<CustomClass1> items1 
List<CustomClass2> items2

CustomClass1 has a property KEY
CustomClass2 has a property KEY

i want to keep only those entries in items1 which have a matching key in items2. How can this be achieved through LINQ?

thanks

like image 466
stackoverflowuser Avatar asked Aug 07 '26 14:08

stackoverflowuser


2 Answers

var res = items1.Join(items2,
                      item1 => item1.Key, 
                      item2 => item2.Key, 
                      (item1, item2) => item1);
like image 159
Femaref Avatar answered Aug 10 '26 10:08

Femaref


var res = items1.Where(a=> items2.Any(c=>c.Key == a.Key));
like image 37
Mo Valipour Avatar answered Aug 10 '26 10:08

Mo Valipour