0

Assume Entity has columns SomeId, DateAt of respective types int, DateTime. Let assume I have parameters collection: {(SomeId_1, DateAt_1),...,(SomeId_N, DateAt_N)} where 1=<N

I want to retrive all rows in Entity table that: {r: (SomeId_1=r.SomeId AND DateAt_1=r.DateAt) OR .. OR (SomeId_N=r.SomeId AND DateAt_N=r.DateAt)}

Is there a way to write Linq query against DbSet?

_context.Entity....

that will produce query like:

SELECT *
FROM Entity
WHERE ( SomeId_1 = SomeId AND DateAt_1 = DateAt)
    OR..
    OR (SomeId_N = SomeId AND DateAt_N = DateAt)

How to achieve this using EF Core 3.1?

REMARK: Using code like below

List<EntityAtPait> pairs = ...
await _context.Entity.
    .Where(f => pairs.Any(x =>
        x.DateAt == f.DateAt 
        && x.SomeId == f.SomeId ))
    .ToListAsync())

throws:
System.InvalidOperationException: 'The LINQ expression 'DbSet<Entity>
    .Where(f => ...
        .Any(x => x.DateAt == f.DateAt && x.SomeId == f.SomeId ))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
lissajous
  • 371
  • 5
  • 17
  • ahh ok nvm then, maybe this will help you if you can compromise the search for a single property first end filter this list with linq afterwards, https://stackoverflow.com/questions/21641016/check-if-list-contains-item-from-other-list-in-entityframework, its an old question but might do the trick if you are able to compromise – Isparia Apr 15 '20 at 10:55
  • @Isparia this (https://stackoverflow.com/questions/21641016/check-if-list-contains-item-from-other-list-in-entityframework) you refer is for single parameter predicate. This does not satisfy requirements described in OP as I have two parameters to check at the same time. – lissajous Apr 15 '20 at 11:04

0 Answers0