At first, I have a variable in my controller to store linq query result as shown below:
var test= from m in db.testTable
join n in db.testTable2
on m.ID equals n.ID into tabA
from a 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 with in-memory objects using LINQ to Objects instead of LINQ to Entities
{
Value = j.Value.ToString(),
Text = j.Text
}).ToList();
}
The above works perfectly and I able to generate the dropdownlist and the ViewData.
Now I'm trying to move this linQ query into another class instead of putting in this controller as shown below:
public class testClass{
public IQueryable<dynamic> TestMethod(){
return from m in db.testTable
join n in db.testTable2
on m.ID equals n.ID into tabA
from a in tabA
join o in db.testTable3
on m.UserID equals o.ID
select new { .................};
}
}
And now calling it in my 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 with in-memory objects using LINQ to Objects instead of LINQ to Entities
{
Value = j.Value.ToString(),
Text = j.Text
}).ToList();
}
May I know what's wrong with the code? At J.ID and J.Description it's now complaining that An expression tree may not contain a dynamic operation