1

{rated、rater、assessment_id}を含むAssessmentテーブルがあります。別のテーブルは{score、assessment_id}を含むCategory_Scoresです。私はこれを評価者ごとに評価されたグループごとに、スコアごとにグループ化しようとしています。現在、この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 に答える