Robert
Robert

Reputation: 2701

Linq with lambda expression: how do IN statement from sql?

In SQL I can use the IN operator:

select * from project where id IN (select f.id_project from funds)

How can I convert this statement to LINQ?

Controller.Project.Where(p => p.id == id_funds); // I can assign only one id funds

Upvotes: 1

Views: 821

Answers (1)

George Duckett
George Duckett

Reputation: 32448

Use the Contains method:

Controller.Project.Where(p => id_funds.Contains(p.id));

Don't think it applies to your question as it appears id_funds links to a db table but...
Note that if id_funds is an object (i.e. a simple array or list etc.), not something in the database and you're not using Linq-To-Objects you may encounter issues with this as i don't think Linq-To-Entities before v 4.0 supports it (see here for a workaround), and Linq-To-SQL has problems if id_funds is a very large list (more than around 2000 items).

Upvotes: 2

Related Questions