【发布时间】:2016-04-18 16:51:47
【问题描述】:
我在控制器中得到了这个查询,我想把查询结果放到视图模型中:
var query = (from d in db.ObjectCategories
join a in db.MatchingObjects on d.Id equals a.ObjectCategoryId into grp3
join b in db.Unlocks
on d.Id equals b.ObjectCategoryId into grp1
from m in grp1.DefaultIfEmpty()
join c in db.Members
on m.StudentId equals c.Id into grp2
from n in grp2.DefaultIfEmpty()
where m.ObjectCategoryId == null
&& n.Id == null
&& n.Id == (int)Session["UserId"]
orderby d.Id
select new LockedCatListViewModel()
{
AnimalCategory = d.CategoryName,
AnimalCategoryId = d.Id,
Animals = d.MatchingObjects
}).AsEnumerable()
.Select(x=> new LockedCatListViewModel()
{
AnimalCategory = x.AnimalCategory,
AnimalCategoryId = x.AnimalCategoryId,
Animals = x.Animals
});
return View(query.ToList());
视图模型:
public class LockedCatListViewModel
{
[Display(Name = "Animal Category Name")]
public string AnimalCategory { get; set; }
public int AnimalCategoryId { get; set; }
public virtual ICollection<MatchingObject> Animals { get; set; }
}
但是,每当我想在视图页面中的模型中循环项目,或者只是将此模型返回到视图页面时,我都会收到以下错误:
我尝试了许多不同的方法,我想知道在我的 LINQ 查询中应该做些什么才能得到我的 LINQ 查询的结果?
通过将 Session 赋值给一个变量来解决问题,并将该变量放入 Linq 中。
我认为这部分的 LINQ 查询不正确
&& n.Id == null
&& n.Id == (int)Session["UserId"]
【问题讨论】:
标签: c# asp.net-mvc linq