views:

308

answers:

1

I have the following that I'd like to sort:

  IQueryable<Map> list;
  list = from item in ctx.MAP
           .Include("C")
           .Include("L")
           .Include("L.DP")
         select item;
  return list.OrderBy(m=>(m.L.DP.Name + m.L.Code));

This works, but it sorts alphabetically - so 12 comes before 9. (Assume Code is a numeric field)

What is the best way to sort this so Code is sorted numerically?

+2  A: 

You probably want to use the ThenBy extension method to be able to sort by multiple fields ;) In your case that would be

 return list.OrderBy(m=>m.L.DP.Name).ThenBy(m => m.L.Code);
Thomas Wanner
Any way to do this if m.L.Code is actually a string, and needs to be converted to an int?
chris
probably int.Parse would work, you can convert it directly in the lambda expression and it is then sorted numerically
Thomas Wanner