Skip to content
Advertisement

How to convert SQL to LINQ where it contains IN keyword in statement

I have got this SQL statement which I am trying to convert to LINQ.

SELECT * 
FROM History 
WHERE Status = 'Created' AND HistoryId IN (1, 2, 3);

I have not been able to do the IN part. Tried following but I am unable to complete it:

var list = new List<int>() { 1, 2, 3};
var result = db.History.Where(x => x.Status == 'Created' && )

How do I write IN part of SQL in LINQ?

Advertisement

Answer

You can use Contains:

var result = db.History.Where(x => x.Status == 'Created' && list.Contains(x.HistoryId))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement