【问题标题】:Returning anonymous types from stored procedure with LINQ2SQL使用 LINQ2SQL 从存储过程返回匿名类型
【发布时间】:2009-03-02 20:57:22
【问题描述】:

考虑以下存储过程:

SELECT * FROM Customers;

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

该过程显然返回了两个结果集,我试图用这个部分类方法检索它们:

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 实体,但是第二个结果集呢?第二个是自定义选择,将多个表中的多个列组合在一起。我没有具有这些属性的实体。

我应该为该结果集创建一个虚拟实体吗?我希望我能以某种方式使用匿名类型进行此类临时查询。

谢谢。

【问题讨论】:

  • 很遗憾看到这个问题没有“有用”的答案,我希望会有一个!

标签: linq-to-sql stored-procedures


【解决方案1】:

一般来说,只有当返回类型为“对象”时,才能从方法中返回匿名类型。然后,调用代码不知道您的匿名类型可能具有哪些属性,除非它使用反射。

是的,您可以使用匿名类型,但您可能不想这样做,因为它不是很有用。

【讨论】:

  • 当你想返回与 json 相同的对象时很有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
相关资源
最近更新 更多