简体   繁体   English

LINQ(L2E)具有存储过程和IQueryable

[英]LINQ (L2E) with stored procedure and IQueryable

Consider a normal Linq expression will be something like this: 考虑一个正常的Linq表达式将是这样的:
(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, 所以,如果我决定将linq查询的第一部分放在存储过程中,
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. 所以对于这个问题,我相信第一种方法只在调用toList()时才发送sql调用。
In another words, the query sent to sql for execution will be 换句话说,发送到sql执行的查询将是

Select * from Person where age=13

But for method 2, how many times will this query be sent for execution? 但是对于方法2,此查询将被执行多少次?
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? 如果它只发送了一次,是否会使调用存储过程变得多余,因为已知存储过程执行速度更快以及查询发送到sql的样式如何?

I am not sure about this one, but PersonContext.sp_RetrievePerson() returns an ObjectResult , which internally uses a DbDataReader. 我不确定这个,但PersonContext.sp_RetrievePerson()返回一个ObjectResult ,它在内部使用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. 这意味着存储过程被调用一次, personQuery.where(x => x.age==13)迭代结果行并过滤掉不匹配的对象。

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. 所以我认为在这种情况下,如果为存储过程提供参数age以过滤数据库中已有的结果,则使用存储过程才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM