2012-03-09 25 views
0

這段代碼的輸出是這樣的如何通過LINQ寫爲了

a 1   
b 12  

我不會出去放像這樣

b 12    
a 1 

查詢:

var x1 = (from v in db3.VoteRecords 
      join v2 in db3.Partis on v.PartiID equals v2.ID 
      where v.ProvinceID == (int)cmbProvience.SelectedValue 
      && v.DistrictID == (int)cmbDistrict.SelectedValue 
      group v by new { v2.PartiName } into g 
      select new 
      { 
       Parti = g.Key.PartiName, 
       Votes = (from vt in g 
         select g.Key.PartiName).Count()   
      }); 
      dataGridView1.DataSource = x1; 

回答

3

您可以添加這在最後

{ 
    Parti = g.Key.PartiName, 
    Votes = (from vt in g 
      select g.Key.PartiName).Count() 

}).OrderByDescending(l =>l.Parti); 

如果您想按Votes列排序。這樣做:

{ 
    Parti = g.Key.PartiName, 
    Votes = (from vt in g 
      select g.Key.PartiName).Count() 

}).OrderByDescending(l =>l.Votes); 

或者,如果你首先要通過Votes通過Parti,然後命令做到這一點:

{ 
    Parti = g.Key.PartiName, 
    Votes = (from vt in g 
      select g.Key.PartiName).Count() 

}).OrderByDescending(l =>l.Parti).ThenByDescending (l =>l.Votes); 

或者,如果你首先要通過Votes,然後下令由Parti做到這一點:

{ 
    Parti = g.Key.PartiName, 
    Votes = (from vt in g 
      select g.Key.PartiName).Count() 

}).OrderByDescending(l =>l.Votes).ThenByDescending (l =>l.Parti);