c# - IEnumerable<> to IList<> -
i using linq query database , returning generic ilist.
whatever tried couldn't convert iqueryable ilist.
here code.
i cannot write simpler , don't understand why not working.
public ilist<iregion> getregionlist(string countrycode) { var query = c in database.regiondatasource (c.countrycode == countrycode) orderby c.name select new {c.regioncode, c.regionname}; return query.cast<iregion>().tolist(); }
this returns list right number of items empty please help, bloqued couple of days now
your select
statement returns anonymous type: new {c.regioncode, c.regionname}
this can't converted iregion
- duck-typing, c# doesn't support.
your linq statement should return type implements iregion
- code should work.
however shouldn't run - cast<iregion>
should throw runtime exception.
basically:
// isn't anonymous, , should cast public class myregion : iregion { public string regioncode {get;set;} public string regionname {get;set;} } public ilist<iregion> getregionlist(string countrycode) { var query = c in database.regiondatasource (c.countrycode == countrycode) orderby c.name select new myregion {regioncode = c.regioncode, regionname = c.regionname}; return query.cast<iregion>().tolist(); }
update
if underlying linq type implements iregion
can lot simpler:
public ilist<iregion> getregionlist(string countrycode) { var query = region in database.regiondatasource region.countrycode == countrycode orderby region.name select region; return query.tolist(); }
Comments
Post a Comment