LINQ (L2E) with stored procedure and IQueryable

 ̄綄美尐妖づ 提交于 2019-12-13 06:23:42

问题


Consider a normal Linq expression will be something like this:
(This is just a sample to make things more understandable)

 IQueryable<Person> personQuery= (from ppl in PersonContext  
                      select ppl).ASQueryable();


List<Person>personList = personQuery.where(x => x.age==13).ToList();

So if I decided to put the 1st part of the linq query inside a stored procedure,
things will work out something like this.

 IQueryable<Person> personQuery= PersonContext.sp_RetrievePerson().ASQueryable();

 List<Person> personList = personQuery.where(x => x.age==13).ToList();

So for the question, I believe that the 1st method only sends the sql call when toList() is called.
In another words, the query sent to sql for execution will be

Select * from Person where age=13

But for method 2, how many times will this query be sent for execution?
If it is only sent 1 time, does it make it redundant to call the stored procedure as stored procedure is known for having a faster execution and how will the query sent to sql look like?


回答1:


I am not sure about this one, but PersonContext.sp_RetrievePerson() returns an ObjectResult, which internally uses a DbDataReader. That means that the stored procedure is called once, the personQuery.where(x => x.age==13) iterates over the resulting rows and filters out not matching objects.

By using stored procedures for this you might get a small performance gain on querying the database, but you have to evaluate each object in the persons table, AFTER reading it from the database. So I think in this scenario using stored procedures only makes sense, if you provide a parameter age to the stored procedure for filtering the results already in the database.



来源:https://stackoverflow.com/questions/5598386/linq-l2e-with-stored-procedure-and-iqueryable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!