Multiple Order by with Linq

Multiple order by in LINQ

This should work for you:

var movies = _db.Movies.OrderBy(c => c.Category).ThenBy(n => n.Name)

How to use orderby with 2 fields in linq?

MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);

How to apply multiple orderby in linq query

var sortedCollection =
from item in collection
orderby item.Property1, item.Property2 descending, item.Property3
select item;

Multiple Order By with LINQ

You can use the ThenBy and ThenByDescending extension methods:

foobarList.OrderBy(x => x.Foo).ThenBy( x => x.Bar)

linq multiple order DESCENDING

You can get the descending ordering by using a different pair of methods:

items.OrderByDescending(y => y.Year).ThenByDescending(m => m.Month); 

Using LINQ query, you can write:

from date in db.Dates
orderby date.Key.year descending, date.Key.month descending
select new { ... }

The trick is that you need only one orderby clause - if you add multiple of these, the list will be re-sorted each time. To sort elements that have the first key equal using another key, you need to separate them using , (orderby is translated to OrderBy or OrderByDescending while , is translated to ThenBy or ThenByDescending).

How can I order by multiple columns using the IQueryable in Cosmos DB?

This isn't an issue with your C# code. If you want to use ORDER BY on a property that property needs to be indexed. For multiple columns it's the same although you need a composite index for all the properties that appear in your ORDER BY clause in the exact same order.

For your use case you would need the following index added to your Cosmos DB container:

"compositeIndexes": [
[
{
"path": "/Author",
"order": "ascending"
},
{
"path": "/Name",
"order": "ascending"
}
]
]

linq order by based on multiple columns and various crietria

Try replace QuestionDataTypeId where value = 0

.OrderBy(x=>x.QuestionDataTypeId==0?int.MaxValue:x.QuestionDataTypeId)
.ThenBy(t=>t.DisplayOrder)


Related Topics



Leave a reply



Submit