2017-06-05 13 views
0

tblから最高のレコードを選択するクエリを作成しました。私は私のクエリは(StudentId = 1、ハイスコア= 0)のような偽のデータを返すTBLにレコードがないかどうかを確認したい
IF NOT EXIST Recoredデータを選択

var queryWin = (from T in ((from tbl_ActPoints in dc.tbl_ActPoints 
       select new 
       { 
        tbl_ActPoints.StudentId, 
        tbl_ActPoints.Score 
       })) 
    group T by new 
    { 
     T.StudentId 
    } into g 
    orderby 
     ((System.Int32?)g.Sum(p => p.Score) ?? (System.Int32?)0) descending 
    select new 
    { 
     g.Key.StudentId, 
     HighScore = ((System.Int32?)g.Sum(p => p.Score) ?? (System.Int32?)0) 
    }).Take(1); 

答えて

0

これを試してみてください:

var queryWin = (from T in ((from tbl_ActPoints in dc.tbl_ActPoints 
       select new 
       { 
        tbl_ActPoints.StudentId, 
        tbl_ActPoints.Score 
       })) 
    group T by new 
    { 
     T.StudentId 
    } into g 
    orderby 
     ((System.Int32?)g.Sum(p => p.Score) ?? (System.Int32?)0) descending 
    select new 
    { 
     g.Key.StudentId, 
     HighScore = ((System.Int32?)g.Sum(p => p.Score) ?? (System.Int32?)0) 
    }).Take(1); 

var result = queryWin.FirstOrDefault(); 

if (result == null) 
    result = new { StudentId = 1, HighScore=0 }; 
+0

ここではこのコードを使用する必要がありますか? –

+0

回答を編集しました – coolswastik

0

をあなたのコードビットのクリーンアップ:

var queryWin = (from tbl_ActPoints in dc.tbl_ActPoints 
       group new 
       { 
        tbl_ActPoints.StudentId, 
        tbl_ActPoints.Score 
       } by tbl_ActPoints.StudentId into g 
       orderby 
        (g.Sum(p => p.Score) ?? 0) descending 
       select new StudentHighScore 
       { 
        g.Key.StudentId, 
        HighScore = (g.Sum(p => p.Score) ?? 0) 
       }).FirstOrDefault() 
        ?? new StudentHighScore { StudentID = 1, HighScore = 0}; 

トリック、および理由coolswastikのコードは、同じであっても性質と、その2つの匿名オブジェクトとして、仕事は常に異なっているので、あなたは、名前のクラス必要はありませんでした:

class StudentHighScore 
{ 
    public int StudentId { get; set; } 
    public int HighScore { get; set; } 
}