Aishwarya
Aishwarya

Reputation: 7

timeout error in executing a linq query

 IQueryable<MailHeader> mailHeader = 
     _ctx.MailHeaders.Where(w => (w.ToReceipientID.Contains(curUser) 
                                    || w.CCReceipientID.Contains(curUser) 
                                    || w.BCCReceipientID.Contains(curUser)) 
                                  && w.UnRead == true && w.IsDraft == false 
                                  && w.IsInbox == true 
                                  && (w.IsApproved == true || w.IsApproved == null));

int unReadMails = mailHeader.Count();

Hi everyone. Logging in for first time when I faced this issue.

While executing the above lines of my C# controller code I was getting time out expression. After looking at previous suggestions I tried making the query IQueryable and execute but still seeing timeout error while executing the count statment. Can anyone help to find the reason?

Upvotes: 0

Views: 2306

Answers (1)

Matt Jordan
Matt Jordan

Reputation: 2181

Your table is too small to cause a real timeout, so, this is most likely a deadlock that is broken only when the operation times out.

Since the LINQ query in your code creates an IQueryable<> object (like a pending query that may be run when needed), the .Count() is probably being called before the query actually runs, although I am not sure why this would be.

Test this by adding .ToList() to your query to force the query to run (docs on IEnumerable<>.ToList() are here: https://msdn.microsoft.com/en-us/library/bb342261(v=vs.110).aspx):

IEnumerable<MailHeader> mailHeader = 
     _ctx.MailHeaders.Where(w => (w.ToReceipientID.Contains(curUser) 
              || w.CCReceipientID.Contains(curUser) 
              || w.BCCReceipientID.Contains(curUser)) 
              && w.UnRead == true && w.IsDraft == false 
              && w.IsInbox == true 
              && (w.IsApproved == true || w.IsApproved == null)).ToList();

int unReadMails = mailHeader.Count();

If this does not time out, then the cause was a deadlock between the .Count() and the actual query. If this still times out, then something else is locking the table and preventing the LINQ request from completing.

Upvotes: 1

Related Questions