Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ/Lambda equivalent of SQL in

Tags:

c#

lambda

linq

I have an IEnumerable that has a list of objects with ids. I want to select those objects whose IDs are 1, 2, 7, 8, 9, 10, and 11. I don't know the LINQ/Lambda equivalent of the equivalent SQL statement (select * where id in (1, 2, 7, 8, 9, 10, 11)).

I tried something like:

var movieratings = new int[] {1, 2, 7, 8, 9, 10, 11};
list.ratings= list.ratings.Select(x => movieratings.Contains(x.Value));

But that gives me a compile error like saying the type arguments cannot be inferred from usage.

like image 491
Jay Sun Avatar asked Oct 27 '11 15:10

Jay Sun


Video Answer


1 Answers

If you are filtering you need to do that in the where clause not the select clause

var movieratings = new int[] {1, 2, 7, 8, 9, 10, 11};
list.ratings = list.ratings.Where(x => movieratings.Contains(x.Value));
like image 118
Kevin Holditch Avatar answered Sep 29 '22 09:09

Kevin Holditch