tags:

views:

14

answers:

1

Hello,

I've 3 columns that I'd like to sort in the Excel way, i.e. sorting the first column, and for each element sorted in the first column, I'll sort the second column, and so on. Let's say I first sort alphabeticaly Countries, then for each country, I sort alphabeticaly cities. How do I do it using Linq2Sql?

Thanks for helping

+2  A: 

You can string together a bunch of sorts.

You need to use OrderBy and ThenBy.

You can keep chaining them together to get deferred sorting until you do the ToList.

List<YourData> places = GetPlaces();
List<YourData> sortedPlaces = places.OrderBy(p => p.Country).
    ThenBy(c => c.City).
    ThenBy(s => s.SomethingElse).
    ToList();

Using query syntax:

var places = from p in db.Places
             orderby p.Country, p.City, p.SomethingElse
             select p;
Kelsey