Can anyone explain what the difference is between:
tmp = invoices.InvoiceCollection .OrderBy(sort1 => sort1.InvoiceOwner.LastName) .OrderBy(sort2 => sort2.InvoiceOwner.FirstName) .OrderBy(sort3 => sort3.InvoiceID);
and
tmp = invoices.InvoiceCollection .OrderBy(sort1 => sort1.InvoiceOwner.LastName) .ThenBy(sort2 => sort2.InvoiceOwner.FirstName) .ThenBy(sort3 => sort3.InvoiceID);
Which is the correct approach if I wish to order by 3 items of data?
Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
Sorts the elements of a sequence in ascending order according to a key. OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) Sorts the elements of a sequence in ascending order by using a specified comparer.
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.
You should definitely use ThenBy
rather than multiple OrderBy
calls.
I would suggest this:
tmp = invoices.InvoiceCollection .OrderBy(o => o.InvoiceOwner.LastName) .ThenBy(o => o.InvoiceOwner.FirstName) .ThenBy(o => o.InvoiceID);
Note how you can use the same name each time. This is also equivalent to:
tmp = from o in invoices.InvoiceCollection orderby o.InvoiceOwner.LastName, o.InvoiceOwner.FirstName, o.InvoiceID select o;
If you call OrderBy
multiple times, it will effectively reorder the sequence completely three times... so the final call will effectively be the dominant one. You can (in LINQ to Objects) write
foo.OrderBy(x).OrderBy(y).OrderBy(z)
which would be equivalent to
foo.OrderBy(z).ThenBy(y).ThenBy(x)
as the sort order is stable, but you absolutely shouldn't:
OrderBy
was designed to be used.The point of OrderBy
is to provide the "most important" ordering projection; then use ThenBy
(repeatedly) to specify secondary, tertiary etc ordering projections.
Effectively, think of it this way: OrderBy(...).ThenBy(...).ThenBy(...)
allows you to build a single composite comparison for any two objects, and then sort the sequence once using that composite comparison. That's almost certainly what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With