linq - ParseException: No property or field '???' exists in type 'DynamicClass' when using GroupBy and Select -
i'm using entityframework system.linq.dynamic , i've defined employee poco class follows:
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; } }
i'm using code groupby country name:
var query = employees.include(e => e.company).include(e => e.country); var groupbyquery = query.groupby("new (country.code groupbyfield)", "new (it xemployee, it.company xcompany, it.country xcountry)"); var selectquery = groupbyquery.select("new (key.groupbyfield, grouping)"); var grouping = selectquery.select("it.grouping") iqueryable<igrouping<dynamicclass, dynamicclass>>; var objects = grouping.first().asqueryable() iqueryable<object>; // line gives me : parseexception: no property or field 'xemployee' exists in type 'dynamicclass' var employees = objects.select("it.xemployee"); it's strange because when dump properties dynamicclass, xemployee valid public property ?
var firstobject = objects.first(); firstobject.gettype().getproperties().dump(); shows 
i've created extension method gets property object , casts required type.
see code extension method "select"
public static class dynamicqueryableextensions { public static ienumerable<tentity> select<tentity>(this ienumerable<object> source, string propertyname) { return source.select(x => getpropertyvalue<tentity>(x, propertyname)); } private static t getpropertyvalue<t>(object self, string propertyname) { var type = self.gettype(); var propinfo = type.getproperty(propertyname); try { return propinfo != null ? (t)propinfo.getvalue(self, null) : default(t); } catch { return default(t); } } } the code used follows:
// line work fine. var employees = objects.select<employee>("xemployee"); employees.dump();
working example, see kendogridbinderex project on github.
Comments
Post a Comment