System.Linq.Dynamicと共に EntityFramework を使用しており、次のように Employee POCO クラスを定義しました。
public class Employee
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Company Company { get; set; }
public Country Country { get; set; }
}
このコードを使用して、国名で GroupBy を実行しています。
var query = Employees.Include(e => e.Company).Include(e => e.Country);
var groupByQuery = query.GroupBy("new (Country.Code as GroupByField)", "new (it AS XEmployee, it.Company AS XCompany, it.Country AS XCountry)");
var selectQuery = groupByQuery.Select("new (Key.GroupByField, it as Grouping)");
var grouping = selectQuery.Select("it.Grouping") as IQueryable<IGrouping<DynamicClass, DynamicClass>>;
var objects = grouping.First().AsQueryable() as IQueryable<object>;
// This line gives me : ParseException: No property or field 'XEmployee' exists in type 'DynamicClass'
var employees = objects.Select("it.XEmployee");
DynamicClass からすべてのプロパティをダンプすると、XEmployee が有効なパブリック プロパティになるため、非常に奇妙です。
var firstObject = objects.First();
firstObject.GetType().GetProperties().Dump();
ショー