2012-07-02 117 views

回答

4

你可以試試這個方法:

var res = ctx.MyTable // Start with your table 
    .GroupBy(r => r.id)/Group by the key of your choice 
    .Select(g => new {Id = g.Key, Count = g.Count()}) // Create an anonymous type w/results 
    .ToList(); // Convert the results to List 
+0

謝謝。我如何通過Count命令? –

+0

@pirezas在'Select'和'ToList'之間添加'.OrderBy(v => v.Count)'。 – dasblinkenlight

2

你可以試試這個

var res = from r in MyTable 
      group p by r.id into grouped 
      select new {id = g.key, total = g.Count()}; 

那麼,當你使用它,只是做ToList() 您也可以以後做select new。 我沒有Visual Studio 2010這裏試試,但是 我認爲它會工作