2009-03-02 27 views
2

考虑下面的存储过程:返回匿名类型的存储过程LINQ2SQL

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

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

感谢。

+1

耻辱看到这个问题没有“有用”的答案,我希望会有一个! – Jon 2010-01-05 04:38:20

回答

0

一般而言,只有当返回类型为“object”时,才可以从方法返回匿名类型。然后,调用代码不知道匿名类型可能具有哪些属性,除非它使用反射。

所以是的,你可以使用匿名类型,但你可能不想,因为它不是很有用。

+0

当你想返回与json相同的对象时它很有用 – om471987 2013-05-26 10:15:47