Puneet Gupta Puneet Gupta - 1 month ago 16
C# Question

Retrieving all records where a particular column value can be an item in a list in c# using EF DBSET

I am trying to do this for quite some time but not able to sort it out. Would appreciate if someone knows how to do this...

I am using EntityFramework and I want to retrieve all records from a Table called points where Description field matches any strings in a list...Kind of, try to do something like the below

List<string> someList = new List<string>();
db.Points.Where(m.Description IN someList)


How to achieve this in C# and EF with the right lambda expression ?

Answer

You can do something like this:

List<string> someList = new List<string>();
db.Points.Where(m => someList.Contains(m.Description))

Multiple Conditions:

db.Points
 .Where(m => someList.Contains(m.Description))
 .Where(m => m.someprop == true)

OR

db.Points
 .Where(m => someList.Contains(m.Description) && m.someprop == true)