1

我有一个表 Assessment,其中包含 {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
4

2 回答 2

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>
于 2012-10-28T03:02:52.110 回答
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();
于 2012-10-28T03:37:38.577 回答