c# - Convert Anonymous type to IEnumerable<> in EF6 -
i developing mvc application using entity framework. want 5 columns table , return them in ienumerable type. code is:
ienumerable<mst> n = new list<mst>(); n = db.msts.select(x => new { x.id, x.code, x.desc, x.l1, x.l2 }).orderby(h => h.code).tolist(); but gives me error
cannot implicitly convert type 'system.collection.generic.list
<anonymous#1>' 'system.collection.generic.ienumerable<<mst>>'
how can solve it?
first don't need tolist() because don't need list:
db.msts .select(x => new { x.id, x.code, x.desc, x.l1, x.l2 }) .orderby(h => h.code) now need type mst. if type ef knew include directly in select:
db.msts .select(x => new mst{ id = x.id, code =x.code, desc = x.desc, l1 =x.l1, l2 =x.l2 }) .orderby(h => h.code) but it's not, need break ef in-memory asenumerable , creation of mst after that:
ienumerable<mst> n = db.msts .select(x => new { x.id, x.code, x.desc, x.l1, x.l2 }).orderby(h => h.code) .asenumerable() .select(x => new mst{ id = x.id, code =x.code, desc = x.desc, l1 =x.l1, l2 =x.l2 }); (if there's reason why need tolist() can use instead of asenumerable(), you're better off placing final tolist() after of that, list of type want).
if using asynchronous code, place after await:
ienumerable<mst> n = (await db.msts .select(x => new { x.id, x.code, x.desc, x.l1, x.l2 }) .orderby(h => h.code) .tolistasync()) .select(x => new mst{ id = x.id, code =x.code, desc = x.desc, l1 =x.l1, l2 =x.l2 });
Comments
Post a Comment