Gabriele D'Onufrio
Gabriele D'Onufrio

Reputation: 424

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();

Upvotes: 3

Views: 5130

Answers (3)

Sandeep Suthar
Sandeep Suthar

Reputation: 57

ObjGetIndexedViewResult.TotRecord.Equals(listDocuments.Distinct().AsNoTracking().Select(x => new { x.id }).Count());

Upvotes: 1

hitesh panwar
hitesh panwar

Reputation: 40

ObjGetIndexedViewResult.TotRecord = listDocuments.AsNoTracking().Select(x => new { x.id }).Count();

use

Distinct().

Upvotes: -1

Gabriele D'Onufrio
Gabriele D'Onufrio

Reputation: 424

sorry for bothering ya, just found the solution!

with .Distinct().

   ObjGetIndexedViewResult.TotRecord = listDocuments.Distinct().AsNoTracking().Select(x => new { x.id }).Count();

Upvotes: 1

Related Questions