c# - EF select performance - Task.FromResult vs ToListAsync -


i trying figure out best way select data via entity framework async await. in following code have 2 options.

the first uses task.fromresult.
second option tries use async await keywords optimize code.

1.

public task<ienumerable<designexample>> executeasync(getallexamplesquery query) {     var designexamples = _copydatadb.designexamples.orderby(de => de.order).select(designexample.mapfromentity);      return task.fromresult(designexamples); } 

2.

public async task<ienumerable<designexample>> executeasync(getallexamplesquery query) {     var designexamples = await _copydatadb.designexamples.orderby(de => de.order).tolistasync();      return designexamples.select(designexample.mapfromentity); } 
  • does second option increase overall performance in relation first?
  • the second option await call database, give me benefits in scenario?

these methods 2 different things.

your former executes call dbcontext synchronously , wraps result in task. means calling thread not yield control caller when making call database. consumers of method may not understand why executeasync blocking thread.

the latter, makes asynchronous call using tolistasync frees calling thread. means caller can more work thread in meantime, , execution resume when query finishes execution.

does second option increase overall performance in relation first?

in order know have measure code, depends on how long takes execute query. note use of async-await have overhead (although minimal gains of code readability) of generating state-machine behind covers. note cannot execute multiple queries same dbcontext.

the second option await call database, give me benefits in scenario?

the main benefit of while query runs, thread free more work. in environments asp.net, means asp.net threadpool free process more incoming requests in meantime, depends on you're trying achieve.

there intriguing discussion on use of async db calls in why ef 6 tutorial use asychronous calls? think you'll find interesting.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -