如何在linq中编写订单

时间:2012-03-09 07:44:07

标签: 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;

1 个答案:

答案 0 :(得分: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);

或者,如果您首先要按Parti订购,然后按Votes订购,请执行以下操作:

{
    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);