Returning anonymous types from stored procedure wi

2019-07-16 01:29发布

问题:

Consider the following stored procedure:

SELECT * FROM Customers;

SELECT Customer.Id, Customer.Name, Order.Total, Order.DateOrdered
FROM Customers INNER JOIN Orders ON Customers.Id = Orders.CustomerId;

The procedure obviously returns two result sets which I'm trying to retrieve with this partial class method:

public partial class DBSproc : DataContext
{
    [Function(Name = "dbo.spGetCustomersAndOrders")]
    [ResultType(typeof(Customer))]
    // What type should I use here for the second resultset?
    [ResultType(typeof(... what here? ...))] 
    public IMultipleResults GetCustomersAndOrders()
    {
        IExecuteResult result =
            this.ExecuteMethodCall(this,
               ((MethodInfo)(MethodInfo.GetCurrentMethod())));

        return (IMultipleResults)(result.ReturnValue);
    }
}

I understand the first result set will be mapped to the Customer entity, but what about the second one? The second one is a custom select, combining multiple columns from several tables. I don't have a entity with these properties.

Should I create a dummy entity just for that resultset? I was hoping I could somehow use anonymous types for such ad-hoc queries.

Thanks.

回答1:

Generally speaking, you can return anonymous types from a method only when the return type is "object". Then, the calling code has no idea what properties your anonymous types might have, unless it uses reflection.

So yes, you can use anonymous types, but you probably don't want to, because it's not very useful.