首先,我的控制器中有一个变量来存储 linq 查询结果,如下所示:
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();
}
以上工作完美,我能够生成下拉列表和 ViewData。
现在我正在尝试将此 linQ 查询移动到另一个类中,而不是放入此控制器中,如下所示:
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 { .................};
}
}
现在在我以前的控制器中调用它:
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();
}
我可以知道代码有什么问题吗?在J.ID和J.Description它现在抱怨表达式树可能不包含动态操作