c# - MVC LinQ IQueryable Anonymous type handling -
at first, have variable in controller store linq query result shown below:
var test= m in db.testtable join n in db.testtable2 on m.id equals n.id taba in taba join o in db.testtable3 on m.userid equals o.id select new { .................}; if (test.count() > 0) { foreach (var k in test) { viewdata["dropdown_xxx"] = test.select(j => new { value = j.id, text = j.description}) .asenumerable() .select(j => new selectlistitem // work in-memory objects using linq objects instead of linq entities { value = j.value.tostring(), text = j.text }).tolist(); }
the above works , able generate dropdownlist , viewdata.
i'm trying move linq query class instead of putting in controller shown below:
public class testclass{ public iqueryable<dynamic> testmethod(){ return m in db.testtable join n in db.testtable2 on m.id equals n.id taba in taba join o in db.testtable3 on m.userid equals o.id select new { .................}; } }
and calling in previous controller:
var test= new testclass().testmethod(); if (test.count() > 0) { foreach (var k in test) { viewdata["dropdown_xxx"] = test.select(j => new { value = **j.id**, text = **j.description**}) .asenumerable() .select(j => new selectlistitem // work in-memory objects using linq objects instead of linq entities { value = j.value.tostring(), text = j.text }).tolist(); }
may know what's wrong code? @ j.id , j.description it's complaining an expression tree may not contain dynamic operation
here dynamic type inside scope of testmethod
. better create new class , return class caller.
public class testclass{ public iqueryable<myclass> testmethod(){ return m in db.testtable join n in db.testtable2 on m.id equals n.id taba in taba join o in db.testtable3 on m.userid equals o.id select new myclass{ .................}; } }
if need return anonymous type check
Comments
Post a Comment