Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
813 views
in Programming by 66 69 85
edited by

Using.NET Core and EF, I'm trying to implement a complex query statement that uses the WHERE IN statement for many statuses, such as:

select * from Requests where RequestStatusId in (1, 2, 3) 

I tried the following code because I needed to convert this query to Linq.Contains

var Items = await _context.Requests.Where(r => r.RequestStatusId.Contains(new[] { 1,2,3}));

But it dosen't works i get error the below error:

  • Argument 1: cannot convert from 'int[]' to 'char'

How do I use WHERE IN statement in Linq C#?


1 Answer

0 like 0 dislike
by 66 69 85
selected by
 
Best answer

Use WHERE IN Statement in Linq C#?

To solve a case like that, simply you need to do the following inside the .Where():

  1. Use Lamda expression
  2. Create your Array
  3. After the closing brackets, use .Contaings()
  4. Inject the RequestStatusId in .Contaings

Just like the below code:

var Items = await _context.Requests.Where(r => new[] { 1, 2, 3, 4, 5}.Contains(r.RequestStatusId));

You will get only the RequestStatusID you need.

If you don’t ask, the answer is always NO!
...