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
C#: Convert Byte Array into a Float
How to Save Picturebox.Image to File
Checking File/Folder Access Permission
Wrap C# Application in .Msi Installer
Using Datetime in a SQLparameter for Stored Procedure, Format Error
When Is Optimization Premature
Class List Keeps Printing Out as Class Name in Console
How to "Zip" or "Rotate" a Variable Number of Lists
Convert from Scientific Notation String to Float in C#
Equivalence of "With...End With" in C#
Internal VS. Private Access Modifiers
Executenonquery for Select SQL Statement Returning No Rows
How to Use Httpwebrequest to Pull Image from Website to Local File
Static Binding Doesn't Update When Resource Changes
Reading a Key from the Web.Config Using Configurationmanager