2010-08-18 198 views
3

当您执行存储过程时,是否可以从LINQ To SQL DataContext获取输出参数?从LINQ中存储过程捕获输出参数到SQL ExecuteQuery

IEnumerable<Address> result = 
    ExecuteQuery<Address>(((MethodInfo)(MethodInfo.GetCurrentMethod())), 
     address, pageIndex, pageSize, totalCount); 

其中addresspageIndexpageSize是输入参数,和TotalCount是一个输出参数。

如何捕获输出参数?

这里是在做另一次尝试却又无法得到的参数值:


    [Function(Name = "Telecom.AddressSearch")] 
     private IEnumerable SearchAddress([Parameter(Name = "address", DbType = "varchar")] string address, 
              [Parameter(Name = "pageIndex", DbType = "int")] int pageIndex, 
              [Parameter(Name = "pageSize", DbType = "int")] int pageSize, 
              [Parameter(Name = "totalCount", DbType = "int")] ref int totalCount) 
     {    
      IEnumerable result = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount); 

      return result; 
     }

回答

6

斯科特·格思里有一个blog post描述如何得到的LINQ to SQL存储过程的工作。虽然他的文章并不直接回答你的问题,但它提供了一些可能有用的线索。在.dbml类中定义方法时,“LINQ to SQL将SPROC中的参数映射为参考参数(ref关键字)”。您可以尝试使用ref关键字并查看totalCount是否得到更新。

IEnumerable r = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), 
    address, pageIndex, pageSize, ref totalCount); 

更新:

我做了一些调查研究,并发现一种方式,应该为你工作。这是来自MSDN article。你需要扩展你的DataContext,但这不应该是一个大问题(看起来你可能已经在这样做)。请查看文章以获得更多信息,但基本部分是这样的:

[Function(Name="dbo.CustOrderTotal")] 
[return: Parameter(DbType="Int")] 
public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")] string customerID, [Parameter(Name="TotalSales", DbType="Money")] ref System.Nullable<decimal> totalSales) 
{ 
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales); 
    totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1))); 
    return ((int)(result.ReturnValue)); 
} 

其中'this'是您的DataContext。

更新2:

的IExecuteResult有ReturnValue属性。它是Object类型的,所以你必须将它转换成结果,但它应该允许你获得结果的Enumerable。在你的情况,这样的事情应该工作:

IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount); 
totalCount = ((System.Nullable<decimal>)(result.GetParameterValue(3))); 
return (IEnumerable<Address>)result.ReturnValue; 
+0

我已经试过了,但它说,PARAMS []对象已经找到另一种可能的解决方案无效参数 – BBurke 2010-08-18 16:02:45

+0

,检查我的更新答案。 – sgriffinusa 2010-08-19 01:17:59

+0

是的,我看了看,但我不认为它会工作。 我看不到IExecuteResult将如何返回Enumerable数据 – BBurke 2010-08-19 09:56:35