2012-03-09 58 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);