0

I'm trying to build a query selecting all records containing IDs which are stored in the list using that code:

var assistsIds = _context.Assistances.Where(c => c.IdUser == user.IdUser)
                        .Select(x => x.Owner.IdOwner).ToList();

Then I'm going through all the list elements to get a query:

var query = _context.Accounts.Where(_ => _.IsDeleted != 1);

                foreach(var assist in assistsIds)
                {
                    query = query.Where(_ => _.IdOwner == assist);
                }

The result is that I'm getting something like this:

SELECT * FROM Accounts WHERE IdOwner = 1 AND IdOwner = 2 ...etc

Instead of:

SELECT * FROM Accounts WHERE IdOwner = 1 OR IdOwner = 2 ... etc

Is there a way to apply OR operator, or maybe there is some other way to achieve that?

Kal800
  • 31
  • 6

1 Answers1

0

You could use Contains:

var query = _context.Accounts
                .Where(_ => _.IsDeleted != 1 && assistsIds.Contains(_.IdOwner));

This will return all records which match an Id in the assistsIds list.

haldo
  • 14,512
  • 5
  • 46
  • 52