Alan2
Alan2

Reputation: 24562

How can I order by more that one element with LINQ /

I have this LINQ statement:

db.PhraseCategories.OrderBy(c => c.Name);

I would like to order first by GroupId and then second by Name.

Can anyone give me advice on how I can do this?

Upvotes: 0

Views: 44

Answers (1)

Hari Prasad
Hari Prasad

Reputation: 16956

You could use ThenBy extension method to performs a subsequent ordering of the elements in a sequence in ascending order according to a key.

db.PhraseCategories
.OrderBy(c => c.GroupId)
.ThenBy(c => c.Name);

Or

from c in db.PhraseCategories         
orderby c.GroupId, c.Name

Upvotes: 4

Related Questions