c# - IQueryable<T> cast to IList<SpecificInterface> -
setup
public interface itable { } public class company : itable { public int id { get; set; } public string name { get; set; } } public class paginationgridmodel { public paginationgridmodel(ilist<itable> rows) { //cool stuff goes here } } public gridmodel generatemodel<t>(iqueryable<t> table) t : itable { return new gridmodel((ilist<itable>)table); } //actual call return generatemodel<company>(this.datacontext.companies);
exception generated
unable cast object of type 'system.collections.generic.list`1[company]' type 'system.collections.generic.ilist`1[itable]'.
question
since company
implements itable
should able convert list<company>
ilist<itable>
doesn't want work because it's t
. t
constrained in function definition itable
. doing wrong here? when i'm not using generics setup works fine. wanted generic setup because i've been writing same code on , on - bad :)
any appreciated. if tell me can't done.
for .net 3.5 can use this:
return new gridmodel(table.tolist().convertall(x => (itable)x));
Comments
Post a Comment