我可以将我的Linq整合到Sql查询结果作为查询的一部分吗?例如,以下查询将返回下面的列表A.我希望我的列表合并如下面的列表B.
IEnumerable<JoinClass> points =
from c in db.Users2
join e in db.Categories on c.Id_Users2 equals e.FK_Users2
join f in db.Programs on e.Id_Category equals f.FK_Categories
join g in db.Points on f.Id_Programs equals g.FK_Programs
where c.EEID == UserEEIDCast
orderby g.EntryDate ascending
select new JoinClass
{
Category = e.Category,
Programs = f.Programs,
Points = g.Points,
EEID = c.EEID,
EntryDate = g.EntryDate,
Name = c.Name
return View(points);
列出A
名称计划点
Jim Running 10
Jim Running 3
Jim Walking 7
Jim Walking 4
Bob Running 2 Bob Running 1
列表B
名称计划点
Jim Running 13
Jim Walking 11
Bob Running 3
答案 0 :(得分:0)
尝试:
points = points
.GroupBy(x => new { x.Name, x.Program } )
.Select(x => new JoinClass() {Name = x.Key.Name, Program = x.Key.Program, Points = x.Sum(y => y.Points) /* copy other properties to the new JoinClass here */ });
此代码使用复合键(名称和程序)对结果进行分组,然后为每个组创建一个新的JoinClass,其Points等于该组中所有点的总和。