Im try to refactor this method to take list of items:
public async Task<bool> IfExist(string id1, string id2)
{
return await DbSet.AnyAsync(x => x.Id1 == id1 && x.Id2 == id2 &&);
}
That method works perfectly and produce SQL
that looks like this:
Command: SELECT CASE
WHEN EXISTS (
SELECT 1
FROM "SomeTable" AS x
WHERE ((x."Id1" = @__id1) AND (x."Id2" = @__id2))
THEN TRUE::bool ELSE FALSE::bool
END
-- Parameters:
@__id1: value
@__id2: value
But now, i want to write IfExist
method that takes lists of ids and checking pairs of id1
and id2
. I wrote something like this:
public async Task<bool> IfExist(IEnumerable<string> ids1, IEnumerable<string> ids2)
{
var ids = ids1.Zip(ids2, (id1, id2) => new { id1, id2 });
return await DbSet.AnyAsync(x => ids.Contains(new { id1 = x.Id1, id2 = x.Id2 }));
}
But its just execute SELECT * FROM ...
on my database so basiclly this query is executed in memory, not in database. EF Core 2.1 cant produce SQL
code from that LINQ
query.
My question is, how to write query that check if any pair of two properties exist in whole table using EF Core 2.1 and LINQ?
Edint when i try to contains existing of Tuple<string, string>
its still not work correctly and executed SQL
looks like this:
Command: SELECT x."Id1", x."Id2"
FROM "SomeTable" AS x
So it means that my query download whole table from DB and make a linq query in memory