2012-04-18 105 views
2

我有一個簡單的數據庫,使用EF數據模型來處理。如何使用從方法返回的匿名類型

我的表是這樣的:

客戶表

  • 客戶編號
  • 客戶名稱

Orders表

  • 的OrderId
  • 客戶ID FK
  • 訂購日期

我使用一個輔助類來查詢我的模型,並在這個類中,我有以下查詢:

public static List<object> GetCustomerOrdersCount() 
{ 
    using (OrdersDbEntities context = new OrdersDbEntities()) 
    { 
     return context.Customers.Select(
      c => new 
      { 
       CustId = c.CustomerId, 
       CustName = c.CustomerName, 
       OrdersCount = c.Orders.Count 
      }).ToList<object>(); 
    } 
} 

的唯一回報I型可以用這種方法使用是List<object>

最後我的問題是:如何做我使用從這個查詢中收到的數據?

我可以讀出的值的唯一方法是通過反射:

List<object> custs = Dal.GetCustomerOrdersCount(); 

foreach (var customer in custs) 
{ 
    var properties = customer.GetType().GetProperties(); 

    foreach (var data in properties) 
    { 
     var value = data.GetValue(custs[0], null); 
    } 
} 

我不知道是否有更好的方法來做到這一點。

+2

爲什麼您使用的對象,而不是客戶?我假設context.Customers是客戶的DbSet? – Maess 2012-04-18 19:31:32

+0

這是我在嘗試返回列表時得到的錯誤:System.Linq。IQueryable '不包含'ToList'的定義和最好的擴展方法重載'System.Linq.ParallelEnumerable.ToList (System.Linq.ParallelQuery )'有一些無效參數 – Yoav 2012-04-18 19:34:58

+0

請發佈代碼OrdersDbEntities – Maess 2012-04-18 19:36:37

回答

1

,我看到的問題是,你選擇一個匿名類型,你不能做的:

public static List<object> GetCustomerOrdersCount() 
{ 
     using (OrdersDbEntities context = new OrdersDbEntities()) 
     { 
      return context.Customers.Select().ToList<Customer>(); 
     } 
} 

,將返回您的完整Customer實體,而不是隻包含特定的成員匿名類型。

編輯

如果底層的問題,關於我們對延遲加載的談話,是要令給定客戶的數量的計數,而無需實際加載的訂單,那麼我會做這樣的事情:

public class CustomerWithOrderCount 
{ 
    public CustomerWithOrderCount(Customer c, int OrderCount) 
    { 
     Customer = c; 
     this.OrderCount = OrderCount; 
    } 
    public Customer { get; set; } 
    public int OrderCount { get; set; } 
} 

public static List<object> GetCustomerOrdersCount() 
{ 
     using (OrdersDbEntities context = new OrdersDbEntities()) 
     { 
      return context.Customers.Select(
       c => new CustomerWithOrderCount(c, c.Orders.Count()) 
          .ToList(); 
     } 
} 
+0

這是我在嘗試執行您的建議時得到的錯誤:ObjectContext實例已被處置,不能再用於需要連接的操作。我想我可以做這樣的事情:return context.Customers.Include(「Orders」)。ToList ();但是這會帶來很多不必要的數據...... – Yoav 2012-04-18 19:45:39

+0

是的,你可能試圖做延遲加載。如果你想這樣做,你需要保留用於加載實體的上下文,以便實體可以使用上下文從數據庫中獲取數據。否則使用'.Include()'或'.LoadProperty()'來加載額外的數據是最好的方法。 – CodingGorilla 2012-04-18 19:48:07

+0

或者我可以繼續使用反射:)我會嘗試查找你提到的這個懶加載的東西 – Yoav 2012-04-18 20:00:34

2
public class MiniCustomerDto 
{ 
    public int CustomerId{get;set;} 
    public String CustomerName{get;set;} 
    public int OrdersCount{get;set;} 
} 

public static List<MiniCustomerDto> GetCustomerOrdersCount() 
{ 
     using (OrdersDbEntities context = new OrdersDbEntities()) 
     { 
      return context.Customers.Select(c => new MiniCustomerDto 
      { 
       CustId = c.CustomerId, 
       CustName = c.CustomerName, 
       OrdersCount = c.Orders.Count 
      }).ToList(); 
     } 
} 

你必須使用類,你不能創建返回匿名類型
ABD這是最好的做法通道的方法埃克
Is there a way to return Anonymous Type from method?
Return anonymous type results?
的方式DTO代表數據傳輸對象

0
List<tbl_GameConfig> gameConfig = new List<tbl_GameConfig>(); 
using (Entities con = new Entities()) 
    { 
     gameConfig = con.tbl_GameConfig.Where(p => p.fk_GameTypeId == gameTypeId).ToList<tbl_GameConfig>(); 
    }