2012-10-28 135 views
1

我有一個表的評估,其中包含{額定,評價者,assessment_id}另一個表是Category_Scores含有{得分,assessment_id}。我試圖按照評分者的評分組來分組評分,分組評分。目前,我有這個LINQ查詢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 

回答

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> 
+0

感謝您的答覆,我想在視圖中顯示它,但是難以產生結果,我應該如何在視圖中執行循環? –

+0

@shinratensei:假設你的模型的類型是IEnumerable >,你的當前視圖代碼應該在給定屬性名稱更改時正常工作。你遇到了什麼錯誤? – ChaseMedallion

+0

我的薄霧先生,我忘了添加我的viewmodel後選擇新的。這將評估者按照我想要的評分進行分組,但是我無法在視圖中顯示正確的數據位置。我將編輯我的帖子,向您展示我目前擁有的視圖 –

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(); 
+0

我得到一個傳遞到詞典中的模型項的類型爲「System.Collections.Generic.List'1 [<> f__AnonymousTypea'2 [<> f__AnonymousType9'1 [System.String],System.Collections.Generic.IEnumerable' 1 [System.Linq.IGrouping'2 [System.String,Ca.ViewModel.KyubiViewModel]]]] '但本詞典需要類型的模型項' System.Collections.Generic.IEnumerable'1 [System.Linq.IGrouping '2 [System.Object的,Ca.ViewModel.KyubiViewModel]]」我使用這種類型的@model IEnumerable的> –

+0

@shinratensei:我猜你將需要更改字典的類型。否則,你會如何改變你的數據顯示方式? –