linq - IQueryable to IQuerable<T> -
is possibile convert iqueryable object iqueryable t mapped entity? (t poco class).
thanks in advance.
just cast<t>() it. assuming queryable of same type. otherwise use oftype<t>() filtering method filter out items of type.
iqueryable query = ...; iqueryable<mytype> x = query.cast<mytype>(); // assuming queryable of `mytype` objects iqueryable<myderivedtype> y = query.oftype<myderivedtype>(); // filter out objects derived `mytype` (`myderivedtype`) however in case, using dynamic linq , doing dynamic projection. consider made query:
var query = dc.sometable .where("someproperty = \"foo\"") .select("new (someproperty, anotherproperty)"); it results in query of type iqueryable. cannot cast query of specific type iqueryable<t> after all, t? dynamic linq library creates type derives dynamiccass. cast iqueryable<dynamicclass> (query.cast<dynamicclass>()) not have access properties it's moot.
really nice option have use dynamic access these properties in case.
foreach (dynamic x in query) { string someproperty = x.someproperty; int anotherproperty = x.anotherproperty; // etc... } if want convert query of poco objects, you'll have conversion separate step using linq objects.
ienumerable<somepoco> query = dc.sometable .where("someproperty = \"foo\"") .select("new (someproperty, anotherproperty)") .cast<dynamicobject>().asenumerable().cast<dynamic>() .select(x => new somepoco { someproperty = x.someproperty, anotherproperty = x.anotherproperty, }); if must have iqueryable<t>, should not use dynamic projections in first place.
iqueryable<somepoco> query = dc.sometable .where("someproperty = \"foo\"") .select(x => new somepoco { someproperty = x.someproperty, anotherproperty = x.anotherproperty, }); seeing how cast not working linq entities, suppose option have type collection of poco objects break out loop.
var query = dc.sometable .where("someproperty = \"foo\"") .select("new (someproperty, anotherproperty)"); var result = new list<somepoco>(); foreach (dynamic x in query) { result.add(new somepoco { someproperty = x.someproperty, anotherproperty = x.anotherproperty, }); }
Comments
Post a Comment