skip to Main Content

The background task I am running in my asp.net application is taking long time to execute and is getting stuck at a linq query.How to optimize the linq query or can you suggest some alternatives so that this method or this linq can be speeded up.

Linq is something like this:

 Litigation i = litigationsNotClosed.SingleOrDefault(x => x.Id_local == d.Id_local && x.LegalEntityID_local == importEntity);

2

Answers


  1. Make sure litigationsNotClosed variable is a IQueryable and not a materialized result. Try to change the code to:

    Litigation i = litigationsNotClosed.Where(x => x.Id_local == d.Id_local && x.LegalEntityID_local == importEntity).SingleOrDefault();
    

    An explanation will be posted if it works.

    Login or Signup to reply.
  2. Does your Linq query use EF DbContext to connect to SQL? How do you provide the context? I think if you create DbContext in a using statement inside of your background task, it may solve your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search