LINQ / Lambda SQL equivalent in

I have an IEnumerable that has a list of objects with identifiers. I want to select those objects whose identifiers are 1, 2, 7, 8, 9, 10 and 11. I do not 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 this gives me a compilation error, for example, saying that type arguments cannot be taken out of use.

+6
source share
1 answer

If you are filtering, you need to do this in the where clause, and not in the select clause

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

Source: https://habr.com/ru/post/900210/


All Articles