c# - LINQ-Expression fails but foreach loop works Entity-Framework -


my application asp.net core 1.0 web api.

i have following linq-expression in code:

int number = 0; var orders = await this.datarepo.where(data => data.number == number).tolistasync(); 

if try run code fails , gives me following error message:

microsoft.entityframeworkcore.query.internal.sqlserverquerycompilationcontextfactory:error: exception occurred in database while iterating results of query. system.data.sqlclient.sqlexception: timeout expired. timeout period elapsed prior completion of operation or server not responding. ---> system.componentmodel.win32exception: der wartevorgang wurde abgebrochen --- end of inner exception stack trace --- @ system.data.sqlclient.sqlcommand.<>c.b__107_0(task`1 result) @ system.threading.tasks.continuationresulttaskfromresulttask`2.innerinvoke() @ system.threading.tasks.task.execute() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.storage.internal.relationalcommand.d__20.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asyncqueryingenumerable.asyncenumerator.d__8.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.selectasyncenumerable`2.selectasyncenumerator.d__4.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.selectasyncenumerable`2.selectasyncenumerator.d__4.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.exceptioninterceptor`1.enumeratorexceptioninterceptor.d__5.movenext() clientconnectionid:64923a0e-cf94-487c-be83-a43b719d8c45 error number:-2,state:0,class:11

system.data.sqlclient.sqlexception: timeout expired. timeout period elapsed prior completion of operation or server not responding. ---> system.componentmodel.win32exception: der wartevorgang wurde abgebrochen --- end of inner exception stack trace --- @ system.data.sqlclient.sqlcommand.<>c.b__107_0(task`1 result) @ system.threading.tasks.continuationresulttaskfromresulttask`2.innerinvoke() @ system.threading.tasks.task.execute() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.storage.internal.relationalcommand.d__20.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asyncqueryingenumerable.asyncenumerator.d__8.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.selectasyncenumerable`2.selectasyncenumerator.d__4.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.selectasyncenumerable`2.selectasyncenumerator.d__4.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.exceptioninterceptor`1.enumeratorexceptioninterceptor.d__5.movenext() clientconnectionid:64923a0e-cf94-487c-be83-a43b719d8c45 error number:-2,state:0,class:11 ausnahme ausgelöst: "system.data.sqlclient.sqlexception" in system.private.corelib.ni.dll

however, if change linq-expression following loop works fine:

var orders = new list<exampleclass>(); int number = 0;  foreach (var data in datarepo) {    if (data.number == number)    {       orders.add(data);    } } 

i havent seen same problem..

does know why case? thank much

since timeout reported sql client, guess it's not problem linq expression execution of compiled sql statement against database. exception gives 2 hints:

  • the timeout period elapsed prior completion of operation
  • the server not responding

microsoft.entityframeworkcore.query.internal.sqlserverquerycompilationcontextfactory:error: exception occurred in database while iterating results of query. system.data.sqlclient.sqlexception: timeout expired. timeout period elapsed prior completion of operation or server not responding.

regarding fact second code loads entire table memory within timeout period, can exclude second option.

the default timeout of sql connection 15 seconds. should appropriate execute simple queries 1 code generate. when loading of entire table faster filtering single parameter in clause, there problem indexing:

  • missing index on 'number'
  • the index highly fragmented or statistics not date
  • the index not appropriate because thousands of rows have 'number' = 0
  • the clustered index of table fragmented
  • other reasons

to focus issue in database, extract sql query - either tracing sql string in debugger or using sql server profiler - execute in sql server management studio , analyze execution plan further information.

to exclude side effects of asynchronous query execution, doing tests in default synchronous context.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -