Alan Alan - 1 year ago 67
C# Question

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?

Answer Source

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

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


from c in db.PhraseCategories         
orderby c.GroupId, c.Name
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download