我正在尝试显示查询的结果,难点在于表中的每一行都包含一个Route对象和一个Visit对象列表。我想将查询结果保存在RouteViewModel列表中:
public class RouteViewModel
{
public Route Route { get; set; }
public List<Visit> Visits { get; set; }
}
我运行查询并尝试保存结果:
var query =
from r in db.Routes
join v in visitList on r equals v.Route into visits
select new { RouteObject = r, VisitList = visits };
int i = 0;
foreach (var prodGroup in query)
{
vm[i].Route = prodGroup.RouteObject; //vm is an list of RouteViewModels.
j = 0;
foreach(var prodItem in prodGroup.VisitList)
{
vm[i].Visits[j] = prodItem;
j++;
}
i++;
}
我在类似帖子的答案之后编写了foreach循环,但该行给了我错误:
无法创建“ApplicationDatabase.Visit”类型的常量值。此上下文仅支持原始类型或枚举类型
如何访问路线及其访问列表?
答案 0 :(得分:0)
如果您是
,我不确定您是否使用EF如果Route表中有Primarykey,则使用它来比较对象。喜欢
var query =
from r in db.Routes
join v in visitList on r.Id equals v.Route.Id into visits
select new { RouteObject = r, VisitList = visits };