asp.net mvc - Type of Iqueryable at runtime -
i have method returns iqueryable result, result based on if else condition, if condition satisfies use "assetdetails" class object ,otherwise "userandclientdetails" object. here code:
private iqueryable<?> getassetdetails(shareviewmodel item) { ... if (type == "video") { if (type == "video") { return meta in my.assets().oftype<model.video>() join content in my.contents() on meta.contentid equals content.id join channel in my.channels() on content.channelid equals channel.id meta.id == item.id select new assetdetails { contenttitle = content.title, channelname = channel.channelname, ... }; } else { return meta in my.assets().oftype<model.client>() join country in db.countries on meta.resellercountry equals country.id meta.id == item.id select new userandclientdetails { name = meta.resellername, username = meta.reselleremail, .. };}
so how decide type of iqueyable here @ runtime??
so, able verify works, i'll go ahead , post answer.
you can return iqueryable
instead of generic iqueryable<>
. accept iqueryable<t>
. however, iqueryable
, since has no direct inner type, very limited. so, you'll still need cast iqueryable<>
@ other point in code done:
// piece of code know working `iqueryable<assetdetails>` iqueryable<assetdetails> assetdetails = getassetdetails(someitem);
that's little dangerous, though, you're assuming code working , right type of thing being returned. better be:
try { var assetdetails = (iqueryable<assetdetails>)getassetdetails(someitem); // `assetdetails` } catch (invalidcastexception) { // recover gracefully }
Comments
Post a Comment