Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting in Entity Framework

Is there a way to keep efficient counting on Entity Framework? Please, any help would be appreciated.

I am trying to count rows on an EF query, but the count goes in timeout, nevertheless the query itself it is ok.

Tried with .AsNoTracking() also.

Example:

ObjGetIndexedViewResult.TotRecord = listDocuments.AsNoTracking()
                                                 .Select(x => new { x.id })
                                                 .Count();
like image 813
Gabriele D'Onufrio Avatar asked Nov 26 '25 23:11

Gabriele D'Onufrio


1 Answers

sorry for bothering ya, just found the solution!

with .Distinct().

   ObjGetIndexedViewResult.TotRecord = listDocuments.Distinct().AsNoTracking().Select(x => new { x.id }).Count();
like image 150
Gabriele D'Onufrio Avatar answered Nov 29 '25 12:11

Gabriele D'Onufrio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!