存储过程和Linq-to-sql [英] Stored procs and Linq-to-sql
问题描述
有人可以告诉我在使用存储过程时linq-to-sql的执行计划是如何工作的。
在下面的例子中,生成的SQL语句会是什么样子?
SP使用
Can some one please tell me how linq-to-sql's execution plan works when using stored procedures.
In the following example,what would the generated SQL statements look like?
The SP to use
CREATE PROCEDURE dbo.TestLINQ
AS
BEGIN
SELECT PartID, Description FROM Part
END
调用它的C#
The C# that calls it
using (DataContextX db = new DataContext())
{
var qry = db.TestLINQ();
someCtrl.DataSource = qry.Where(x => x.PartID < 100).OrderBy(x => x.Description);
someCtrl.DataBind();
}
首先调用EXEC dbo.TestLINQ并将其加载到某个临时表(或CTE)中,然后过滤结果,然后对结果进行排序?或者什么?
我有一堆现有的存储过程需要动态的额外过滤。所以我试图确定LINQ是否是一种有效的使用方法。或者我应该走很长的路,并在存储过程中添加一堆if语句和新参数?
Would this first call EXEC dbo.TestLINQ and load it into some temporary table (or CTE), and then filter the results, then sort the results? Or what?
I've got a bunch of existing stored procedures that need additional filtering on the fly. So I'm trying to establish if LINQ is an efficient method to use. Or should I take the long road and add a stack of if statements and new parameters to the stored procedures?
推荐答案
当您使用EF调用该过程时,它将加载收集的结果可能是一些匿名对象。
然后您可以根据条件使用linq优先使用结果集
When you call the procedure using the EF it will load the result in collection of may be some anonymous object.
You can then use the filter on the result set as per the conditions using linq preferably
这篇关于存储过程和Linq-to-sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!