5

Possible Duplicate:
Multiple “order by” in LINQ

I want to order some dataset by some field AND THEN some other field.

Using lambda expressions this would be easy (OrderBy.ThenBy), but how to do it when I have a syntax like this:

 int maxQueries = int.MaxValue;
        // finds the most search for queries
        var ordered = from p in searchLogs
        where !string.IsNullOrEmpty(p.SearchQuery)
        group p by new 
        { 
           SearchQuery = p.SearchQuery
        } 
        into pgroup
        let count = pgroup.Count()
        orderby count descending
        select new 
        { 
            Count = count, 
            SearchQuery = pgroup.Key.SearchQuery
        };

I can't seem to find a way which works after the decending keyword (like orderby count descending then searchquery)..

Any ideas?

Community
  • 1
  • 1
Lars Holdgaard
  • 8,386
  • 20
  • 85
  • 170

2 Answers2

7

Put a comma after descending and specify the next thing to sort by (optionally adding descending) and so on

mlorbetske
  • 5,249
  • 1
  • 24
  • 39
0

Just to add some code to mlorbetske answer, if you have Customer class like this:

public class Customer
{
    public string Name;
    public DateTime MemberSince;
}

You could then perform an ordering like this:

var q = from c in Customers
        orderby c.MemberSince.Date descending, c.Name
        select c;
Thomas C. G. de Vilhena
  • 11,881
  • 2
  • 42
  • 39