Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search between Two IEnumerable - LINQ

Hi can you guys help me with this, I have try several things. I need to search between two IEnumerables, here is the code.

IEnumerable<Project> Projects = new[] { new Project {id = "1", lan = "test1"},  new Project {id = "2", lan = "test1"}}

IEnumerable<string> lan = new [] { "test1", "test2"};
IEnumerable<string> indexFiltered = ?;

I need to do a linq query that return the Project.id thats have any Project.lan in lan.

Any idea?

like image 958
Don Juan Avatar asked Dec 30 '25 00:12

Don Juan


2 Answers

I'd use a HashSet rather than an array, as it allows check-if-contains as an O(1), rather than O(n), operation:

HashSet<string> lan = new HashSet<string> { "test1", "test2" };
IEnumerable<string> indexFiltered = projects
    .Where(p => lan.Contains(p.lan))
    .Select(p => p.id);
like image 64
Rawling Avatar answered Jan 01 '26 13:01

Rawling


 var results = projects.Where(p => lan.Contains(p.lan));
like image 40
Mahmoud Gamal Avatar answered Jan 01 '26 12:01

Mahmoud Gamal