具有多个结果的Linq到SQL存储过程

时间:2022-03-15 02:09:43

I have a stored procedure that selects from multiple tables

我有一个从多个表中选择的存储过程

The calling method in the class the inherits from DataContext is as follows

从DataContext继承的类中的调用方法如下

[Function(Name = "dbo.BASE_Product_Retrieve")]

[功能(名称=“dbo.BASE_Product_Retrieve”)]

    [ResultType(typeof(BASE_Product))]

    [ResultType(typeof(BASE_Product_Version))]

    [ResultType(typeof(BASE_ItemPrice))]

    [ResultType(typeof(BASE_VendorItem))]

    [ResultType(typeof(BASE_ProductAttachment))]

    [ResultType(typeof(BASE_ItemPrice_Version))]


    public IMultipleResults BASE_Product_Retrieves([Parameter(Name = "ProductId", DbType = "Int")] System.Nullable<int> productId, [Parameter(Name = "Version", DbType = "Int")] System.Nullable<int> version)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), productId, version);
        return ((IMultipleResults)(result.ReturnValue));
    }

when i call it from my class

当我从班上打电话的时候

var x=RetrieveProductList.GetResult < BASE_ItemPrice > ();

var x = RetrieveProductList.GetResult ();

it gives me the following exception

它给了我以下例外

The required column 'ItemPriceId' does not exist in the results.

结果中不存在必需的列'ItemPriceId'。

knowing that ItemPriceId is a clumn that exists in that table!!

知道ItemPriceId是该表中存在的一个clumn !!

3 个解决方案

#1


How exactly are you using it? IIRC, you need to access the grids in the right order (it is just an IDataReader, ultimately).

你究竟是如何使用它的? IIRC,您需要以正确的顺序访问网格(最终它只是一个IDataReader)。

Or to quote: "Retrieves the next result as a sequence of a specified type."

或者引用:“将下一个结果作为指定类型的序列检索。”

See also the note on MSDN:

另请参阅MSDN上的说明:

You must use the GetResult(TElement) pattern to obtain an enumerator of the correct type, based on your knowledge of the stored procedure.

必须使用GetResult(TElement)模式根据您对存储过程的了解获取正确类型的枚举器。

So if you want the 3rd grid, I expect you need to call GetResult twice (with appropriate types, and discard them) before you can read the 3rd grid.

因此,如果您想要第3个网格,我希望您需要在读取第3个网格之前调用GetResult两次(使用适当的类型并丢弃它们)。

#2


from the form i hav RetrieveProductList of type RetrieveProductList.

从形式iRev RetrieveProductList类型RetrieveProductList。

i call it as follows var x=RetrieveProductList.GetResult < BASE_ItemPrice > ();

我把它称为如下var x = RetrieveProductList.GetResult ();

this method is in a class that i use in my form

这个方法是在我的表单中使用的类中

public IMultipleResults RetriveProducts((int ItemId, int Version)

public IMultipleResults RetriveProducts((int ItemId,int Version)

    {
        return db.BASE_Product_Retrieves(ItemId, Version);
    }

and The calling method in the class the inherits from DataContext is as follows

从DataContext继承的类中的调用方法如下所示

[Function(Name = "dbo.BASE_Product_Retrieve")]

[功能(名称=“dbo.BASE_Product_Retrieve”)]

    [ResultType(typeof(BASE_Product))]

    [ResultType(typeof(BASE_ItemPrice))]

    [ResultType(typeof(BASE_VendorItem))]

    [ResultType(typeof(BASE_ProductAttachment))]


    public IMultipleResults BASE_Product_Retrieves([Parameter(Name = "ProductId", DbType = "Int")] System.Nullable<int> productId, [Parameter(Name = "Version", DbType = "Int")] System.Nullable<int> version)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), productId, version);
        return ((IMultipleResults)(result.ReturnValue));
    }

while debugging when it reaches

在调试到达时

var x=RetrieveProductList.GetResult < BASE_ItemPrice > ();

var x = RetrieveProductList.GetResult ();

it gives me that exception!!!

它给了我那个例外!!!

#3


i applied what you told me and it somehow worked,the exception is no longer exist but the problem now is that it did not return any value,i used a data grid view an set its data source to

我应用你告诉我的东西,它以某种方式工作,异常不再存在,但现在的问题是它没有返回任何值,我使用数据网格视图设置其数据源

RetrieveProductList.GetResult < BASE_ItemPrice > ();

RetrieveProductList.GetResult ();

it returns the columns,but empty!!!!

它返回列,但是空!!!!

#1


How exactly are you using it? IIRC, you need to access the grids in the right order (it is just an IDataReader, ultimately).

你究竟是如何使用它的? IIRC,您需要以正确的顺序访问网格(最终它只是一个IDataReader)。

Or to quote: "Retrieves the next result as a sequence of a specified type."

或者引用:“将下一个结果作为指定类型的序列检索。”

See also the note on MSDN:

另请参阅MSDN上的说明:

You must use the GetResult(TElement) pattern to obtain an enumerator of the correct type, based on your knowledge of the stored procedure.

必须使用GetResult(TElement)模式根据您对存储过程的了解获取正确类型的枚举器。

So if you want the 3rd grid, I expect you need to call GetResult twice (with appropriate types, and discard them) before you can read the 3rd grid.

因此,如果您想要第3个网格,我希望您需要在读取第3个网格之前调用GetResult两次(使用适当的类型并丢弃它们)。

#2


from the form i hav RetrieveProductList of type RetrieveProductList.

从形式iRev RetrieveProductList类型RetrieveProductList。

i call it as follows var x=RetrieveProductList.GetResult < BASE_ItemPrice > ();

我把它称为如下var x = RetrieveProductList.GetResult ();

this method is in a class that i use in my form

这个方法是在我的表单中使用的类中

public IMultipleResults RetriveProducts((int ItemId, int Version)

public IMultipleResults RetriveProducts((int ItemId,int Version)

    {
        return db.BASE_Product_Retrieves(ItemId, Version);
    }

and The calling method in the class the inherits from DataContext is as follows

从DataContext继承的类中的调用方法如下所示

[Function(Name = "dbo.BASE_Product_Retrieve")]

[功能(名称=“dbo.BASE_Product_Retrieve”)]

    [ResultType(typeof(BASE_Product))]

    [ResultType(typeof(BASE_ItemPrice))]

    [ResultType(typeof(BASE_VendorItem))]

    [ResultType(typeof(BASE_ProductAttachment))]


    public IMultipleResults BASE_Product_Retrieves([Parameter(Name = "ProductId", DbType = "Int")] System.Nullable<int> productId, [Parameter(Name = "Version", DbType = "Int")] System.Nullable<int> version)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), productId, version);
        return ((IMultipleResults)(result.ReturnValue));
    }

while debugging when it reaches

在调试到达时

var x=RetrieveProductList.GetResult < BASE_ItemPrice > ();

var x = RetrieveProductList.GetResult ();

it gives me that exception!!!

它给了我那个例外!!!

#3


i applied what you told me and it somehow worked,the exception is no longer exist but the problem now is that it did not return any value,i used a data grid view an set its data source to

我应用你告诉我的东西,它以某种方式工作,异常不再存在,但现在的问题是它没有返回任何值,我使用数据网格视图设置其数据源

RetrieveProductList.GetResult < BASE_ItemPrice > ();

RetrieveProductList.GetResult ();

it returns the columns,but empty!!!!

它返回列,但是空!!!!