Sort with multiple fields in LINQ

Those crazy users – they wanted to sort by two fields! What madness is that? So I dig into the source and the original code was sorting with LINQ. Being a sheep, I decided to do the same thing. But how? Through the miracle of the ThenBy operator.

So in this example, I wanted to order by City, then Zip Code.  Assuming that my items collection contains objects with City and ZipCode fields:

List<Address> items = DBStuff.GetAddresses(bizID);
GridView1.DataSource = items.OrderBy(i => i.City).ThenBy(i => i.ZipCode);

See MSDN for more info.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: