Home > Mobile >  Linq Count() timing out -Execution Timeout Expired. The timeout period elapsed prior to completion o
Linq Count() timing out -Execution Timeout Expired. The timeout period elapsed prior to completion o

Time:10-15

I have a linq query which tries to fetch approximately 500K records from DB. I have a Count() which eventually timing out.

I want to know if my linq query contains 5000 or more records or not. I don't count of all records, just need to check if linq contains 5000 records.

Is there any effective way to check if there 5000 or more records in linq without calling Count()? I am using EF core 3.1.

Linq Query :

  var results = (from a in RepoContext.Employee
                          join b in RepoContext.Program on a.ProgramId equals b.ProgramId 
                          where a.ActiveFlag == true
                                && b.ClientId == 2
                          select new RAManufacturerDto
                          {

                              BusinessName = a.BusinessName,
                              ClientId = a.ClientId.Value,
                              ClientName = b.ClientName
                              DCode = b.DCode,
                              StoreId = b.StoreId,
                              ProgramId = a.ProgramId
                          });

bool isRecordsLimitReached = results.Count() > 5000;

I am getting an error when trying to do Count() on result. I just want to get if it contains more than 5000 records.

CodePudding user response:

Use linq directly instead, suppose you have your dbcontext properly configured and such relation between the two defined in onmodelcreating, move the code into the RepoContext. ( if in doubt to define relations check this out: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api,fluent-api-simple-key,simple-key )

    DbSet<Employee> Employees {get;set;}
    DbSet<Program> Programs {get;set;}

    public bool HasMoreThan(int number) {
      var count = RepoContext.Employee.Include(x => x.Program).Count(y => 
      y.ActiveFlag == true && y.Program.ClientId == 2);
      return count >= number;
    }

CodePudding user response:

Just try to skip 5000 records and if there are records - we have reached our goal:

bool isRecordsLimitReached = results.Skip(5000).Any();
  • Related