我有一个表格评估,其中包含{rated,rater,assessment_id}另一个表格是Category_Scores,其中包含{score,assessment_id}。我试图按评分对评分组进行分组。目前我有这个LINQ查询
var query = (from a in db.Assessments
join u in db.Users on a.rated equals u.user_id
join u2 in db.Users on a.rater equals u2.user_id
join tm in db.Team_Members on u.user_id equals tm.user_id
join t in db.Teams on tm.team_id equals t.team_id
join c in db.Category_Scores on a.assessment_id equals c.assessment_id
join cat in db.Categories on c.category_id equals cat.category_id
group new KyubiViewModel
{
rated = u.firstName + " " + u.lastName,
rater = u2.firstName + " " + u2.lastName,
score = c.score,
category_name = cat.name,
categoryid = c.category_id,
}
by new { rated = u.firstName + " " + u.lastName,rater = u2.firstName + " " + u2.lastName }).ToList();
这导致
{ rated = John Will, rater = Jess James}
10 [Math]
10 [Science]
{ rated = John Will, rater = Mike Rand}
7 [Math]
7 [Science]
我希望它看起来像
{ rated = John Will }
rater: Math Science
Jess James 10 10
Mike Rand 7 7
{ rated = Jess James }
rater: Math Science
John Will 9 9
Mike Rand 7 7
这是我的观点
<table>
@foreach (var group in Model)
{
<tr>
<td>@group.Key </td>
</tr>
foreach (var m in group)
{
<tr>
<td>@m.score - [@m.category_name]</td>
</tr>
}
}
</table>
任何帮助都将不胜感激。
在ChaseMedallion的帮助下,我能够在我的视野中看到这一点
{ rated = John Will }
rater:
Jess James 10[Math]
Jess James 10[Science]
Mike Rand 7[Math]
Mike Rand 7[Science]
如果可能,我希望这样做
{ rated = John Will }
rater: Math Science
Jess James 10 10
Mike Rand 7 7
答案 0 :(得分:2)
如果设置关联属性而不是使用连接,则LINQ更容易编写。我想你想要的是这样的:
var results = (
from a in db.Assessments
from cs in a.CategoryScores
select new RatingModel {
ratedName = a.RatedUser.firstName + " " + a.RatedUser.lastName,
raterName = a.RaterUser.firstName + " " + b.RaterUser.lastName,
categoryName = cs.Category.name,
cs.score,
cs.category_id,
})
// bring into memory. It may work fine without this, but sometimes LINQ will perform multiple queries if you try to read in a queryable of groupings
.AsEnumerable()
.GroupBy(t => t.ratedName)
.ToList();
在视图中显示:
<table>
@foreach (var group in Model)
{
var allCategories = group.Select(g => g.categoryName).Distinct().OrderBy(s => s).ToList();
<tr>
<td>rater: </td>
@foreach (var name in allCategories) { <td>@name</td> }
</tr>
<tr>
<td>@group.Key </td>
</tr>
foreach (var groupByRater in group.GroupBy(g => g.raterName))
{
<tr>
<td>@groupByRater.Key </td>
@foreach (var name in allCategories)
{
var rating = groupByRater.FirstOrDefault(r => r.categoryName == name);
<td>@(rating == null ? string.Empty : rating.score)</td>
}
</tr>
}
}
</table>
答案 1 :(得分:1)
尝试在每个分组上调用GroupBy()
:
var query = (from a in db.Assessments
join u in db.Users on a.rated equals u.user_id
join u2 in db.Users on a.rater equals u2.user_id
join tm in db.Team_Members on u.user_id equals tm.user_id
join t in db.Teams on tm.team_id equals t.team_id
join c in db.Category_Scores on a.assessment_id equals c.assessment_id
join cat in db.Categories on c.category_id equals cat.category_id
group new KyubiViewModel
{
rater = u2.firstName + " " + u2.lastName,
score = c.score,
category_name = cat.name,
categoryid = c.category_id
}
by new { rated = u.firstName + " " + u.lastName }
into rated
select new { Rated = rated.Key, Raters = rated.GroupBy(x => x.rater) }
).ToList();