2011-11-12 67 views
-1

var ItemScores =(from a in response.ItemScores where a.LastUpdated == (from d in response.ItemScores where a.Word_Id == d.Word_Id select a.LastUpdated).Max()select a);在LINQ to SQL中,如何使用max执行子查询?

上面的查询应该只返回每个Word的最新ItemScore。 每个单词可以有一个或多个ItemScores,因为多个评分者可以对同一个单词进行评分。 我试图获得一个项目分数列表(每个单词一个),使得每个项目分数是该单词的最近一个分数。

不幸的是,此查询返回所有项目分数(每个单词有多个项目分数),就好像它完全忽略了LastUpdated子查询。

回答

1
var itemScores = response.ItemScores 
         .GroupBy(x => x.Word_Id) 
         .Select(g => g.OrderByDescending(x => x.LastUpdated) 
             .First()); 
+0

谢谢卢克。我刚刚意识到我只是有一个“a.LastUpdated”它应该在子查询中是“d.LastUpdated”。 – Triynko