使用LINQ2SQL从存储过程返回匿名类型 [英] Returning anonymous types from stored procedure with LINQ2SQL

查看:57
本文介绍了使用LINQ2SQL从存储过程返回匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下存储过程:

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);
    }
}

我知道第一个结果集将被映射到Customer实体,但是第二个结果集又如何呢?第二个是自定义选择,将来自多个表的多个列组合在一起.我没有具有这些属性的实体.

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.

谢谢.

推荐答案

通常来说,只有当返回类型为"object"时,才可以从方法中返回匿名类型.然后,除非使用反射,否则调用代码不知道您的匿名类型可能具有哪些属性.

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.

这篇关于使用LINQ2SQL从存储过程返回匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆