使用するEFデータモデルを備えた単純なデータベースがあります。
私のテーブルは次のようになります。
顧客テーブル
- 顧客ID
- 顧客名
注文表
- OrderId
- CustomerID 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>();
}
}
このメソッドで使用できる唯一の戻りタイプは、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);
}
}
これを行うためのより良い方法があるのだろうか。