MariINova
MariINova

Reputation: 343

Entity Framework Core Count unrelated records in another table

I need to count how many records in the tableA are not in the tableA, how to do this with LINQ?

with SQL I do the following way

select count(*) as total from produtoitemgrade g
where g.id not in (select idprodutograde from produtoestoque where idProduto = 12)
and g.idProduto = 12

my linq code so far.

 var temp = (from a in Produtoitemgrades
              join b in Produtoestoques on a.IdUnico equals b.IdUnicoGrade into g1
              where g1.Count(y => y.IdProduto == 12)>0 && !g1.Any()
              select a).ToList();

I tried to follow that example LINQ get rows from a table that don't exist in another table when using group by?

but an error occurs when running, how can I do this? Thanks!

Upvotes: 0

Views: 224

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27471

Your query should looks like the following, if you want to have the same SQL execution plan:

var query = 
   from a in Produtoitemgrades
   where !Produtoestoques.Where(b => a.IdUnico == b.IdUnicoGrade && b.idProduto == 12).Any() 
     && a.idProduto == 12
   select a;

var result = query.Count();

Upvotes: 1

Related Questions