这有效:
var model =
(from t in _db.Tickets
from p in _db.Cust
where t.Id == 5 && t.Id == p.cust_id
orderby t.Id descending
select new tks_custs
{
Tks = t,
Custs = p
});
但我希望通过以下方式实现这样的目标:
var model = _db.Tickets
.Join(_db.Cust,
t => t.Id,
w => w.cust_id,
(t, w) => new { Tks = t, Custs = w })
.Where(t => t.Tks.Id == 5).OrderByDescending(w => w.Tks.Id)
.GroupBy(t => t.Tks.Name).ToList();
我的观点声明目前是:
@model IQueryable<test.ViewModels.tks_custs>
答案 0 :(得分:1)
您应该像第一次查询一样创建VieModel对象(tks_custs
)。现在您尝试传递DB模型(实际上不是BD模型,而是匿名IGrouping
对象)。
它应该是这样的:
var model = _db.Tickets
.Join(_db.Cust,
t => t.Id,
w => w.cust_id,
(t, w) => new { Tks = t, Custs = w })
.Where(t => t.Tks.Id == 5).OrderByDescending(w => w.Tks.Id)
.GroupBy(t => t.Tks.Name).ToList()
.Select(x => new tks_custs //here you creating your ViewModel
{
Tks = x.FirstOrDefailt().Tks,
Custs = x.FirstOrDefailt().Custs
});
您可能会注意到FirstOrDefailt()
。你需要它,因为GroupBy
你可能在组中有很多实体。