user1191013
user1191013

Reputation: 9

How to write order by in linq

This code output is like this

a  1          
b  12     

I wont to get out put like this

b  12             
a  1

Query:

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;

Upvotes: 0

Views: 347

Answers (1)

Arion
Arion

Reputation: 31239

You can add this at the end

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

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

If you want to order by the Votes column. Do this:

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

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

Or if you first want to order by Parti and then by Votes do this:

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

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

Or if you first want to order by Votes and then by Parti do this:

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

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

Upvotes: 3

Related Questions