LINQ with an IN clause

I am trying to use "contains" in my LINQ query, but dont see "contains" keyword in LINQ. I have the following table called "Document" in the sql dataabase:

Row1  Row2
1     X
2     Y
3     Z

I want to achieve this in LINQ:

select count(*) from document where Row1 in (1,2,3)

I wrote LINQ this way:

var count = await ( from doc in _Context.Document
                            where doc.Row1.contains

I get an error saying int? does not contain the defination for "contains". Row1 is defined as an integer in the database. I need to put multiple values for Row1.

any help will be appreciated.

>Solution :

The Contains method is available for a List, so your code should look like this:

var list = new List<int> {1,2,3};
var count = await ( from doc in _Context.Document
                            where list.Contains(doc.Row1) );

Leave a Reply