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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.