Guarav T.
Guarav T.

Reputation: 458

To get records in comma seperated List LINQ to Entities

I have a table in database called Products and which has a column name called LaunchYear(int) data type.I want to filter the table using a CSV list of Launch Years.

listCSVLaunchYear= "5,7,8";

I want help to create a LINQ to Entity query which will return me all products in CSV list. I tried using contains but that did not work. Any help will appreciated.

Upvotes: 1

Views: 101

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205769

You need first to convert the CSV list to underlying column data type list and then use Contains, like this

var launchYears = listCSVLaunchYear.Split(',').Select(x => int.Parse(x)).ToList();
var query = db.Products.Where(p => launchYears.Contains(p.LaunchYear));

Upvotes: 1

Related Questions