Linq Query with Multiple Order Fields in Method Syntax

Ordering your sequences is simple using comprehension syntax with your Linq queries, but what if you want to use method syntax?

Here is a simple example with multiple fields in the order by statement.

var movies = from m in Movies
orderby m.CategoryID, m.Name
select m;

Now to accomplish the same thing in method syntax you just use apply a second ordering method like so.

var movies = db.Movies.Orderby(c => c.Category).ThenBy(n => n.Name);

Here is where I found an example for this on stackoverflow.

http://stackoverflow.com/questions/298725/multiple-order-by-in-linq

Advertisements
Tagged with:
Posted in Development

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: