Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

Document
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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading