The way the parser works is that OrderBy is converted to an expressiontree, with nested calls to OrderBy/OrderByDescending/ThenBy/ThenByDescending internally
Could add a shorthand OrderByDescending that can only handle one column though, it's probably not required though.
I don't think I can make it work directly like LINQ: e.g: Model.umbTextPage.OrderByDescending("Id").ThenBy("Name") , although i'll investigate it
.OrderBy("CreateDate desc") and .OrderBy("CreateDate")
.OrderBy("UpdateDate desc") and .OrderBy("UpdateDate")
.OrderBy("Name desc") and .OrderBy("Name")
.OrderBy("Date desc") and .OrderBy("Date") (a property with an alias of "date")
.OrderBy("Heading desc") and .OrderBy("Heading") (a property with an alias of "heading")
The actual value, (but I have stepped through code to look at the value, and it's passed as stated above), are passed in with a PreValue property, DropDown, where I have specifeid the values as described above (camel cased).
Nothing does anything to the resulting list.
This is part of my code: (paste unformatted on a mac is fu**ing impossible)
@helper GetListingFromContent(dynamic listRoot, string orderBy, bool sortByDesc, int itemsPerPage, bool showExpanded) { if (listRoot == null || listRoot.GetType().Equals(typeof(DynamicNull))) { return; } var orderByStatement = String.Empty; if (!String.IsNullOrWhiteSpace(orderBy)) { orderByStatement = String.Concat(orderBy, (sortByDesc ? " desc" : String.Empty)); } var listItems = listRoot.Children .Where("HasAccess") .Where("!IsProtected") .Where("template > 0"); if (!String.IsNullOrWhiteSpace(orderByStatement)) { listItems = listItems.OrderBy(orderByStatement); } var totalItemCount = listItems.Count(); if (totalItemCount <= 0) { return; } if (itemsPerPage.Equals(0)) { itemsPerPage = totalItemCount; }
Sorting a list of items?
I was under the impression that sorting was built in, but when I try something like this:
<umbraco:macro runat="server" language="razor">
@{
var allPages = Model.umbTextpage;
var ordered = allPages.OrderByDescending(x => x.Id);
}
@foreach(var t in ordered){
<p>@t.Id</p>
}
</umbraco:macro>
All I get is a nice error:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Should I be using uQuery / UQL if I need to sort?
Nope, it's built in, just not how you expect it to work... :)
Use Model.umbTextPage.OrderBy("Id")
If you want multicolumn, descending:
Model.umbTextPage.OrderBy("Id, Name descending")
I'm posting some articles to the umbraco blog over the next few days introducing the new features with examples
Cool, but OrderBy("Id descending") throws me an error. OrderBy("Id") works fine.
Maybe it's desc?
What's the error?
"Error loading Razor Script sitemap.cshtmlSyntax error"
Same with "Id desc" I'm afraid.
Okay, must be an issue with starting with a decending sort, I will test and fix
Great, that works (except for descending). Would be best to have it as:
Model.umbTextPage.OrderByDescending("Id")
The way the parser works is that OrderBy is converted to an expressiontree, with nested calls to OrderBy/OrderByDescending/ThenBy/ThenByDescending internally
Could add a shorthand OrderByDescending that can only handle one column though, it's probably not required though.
I don't think I can make it work directly like LINQ: e.g: Model.umbTextPage.OrderByDescending("Id").ThenBy("Name") , although i'll investigate it
It already works when I use the latest code from Codeplex by the way!
Nah, no direct need to add the shorthand then, we just need some good documentation, I know this stuff is hard. :-)
OrderBy("id desc") works with codeplex code but not the 4.7 beta?
I haven't checked the bug/tested the code yet- seems odd
Hm, it is the beta. Umbraco v 4.7.0.beta (Assembly version: 1.0.4071.3793)
Aha, apologies, it didn't actually work completely, it works when I sort multiple colums:
OrderBy("PublishDateOverrule, CreateDate descending")
Hm, shouldnt this be working?
foreach (dynamic n in Model.Children.OrderBy("CreateDate desc"))
{
<p>@n.Name</p>
}
it gives me the same result as
foreach (dynamic n in Model.Children.OrderBy("CreateDate"))
{
<p>@n.Name</p>
}
4.7 release
Same problem here, the list is in descending order by default, and multicolumn searches now give errors (4.7 final).
Guys: Has this been filed as an issue on codeplex or shall i do it?
yes please (I should have, but...)
http://umbraco.codeplex.com/workitem/30198
Thanks Stephan,
I've fixed this in the main trunk, but your workitem says OrderBy("NodeName desc")
Can you confirm if this was meant to be accessing a custom property or the Name property?
They're implemented differently.
Gareth
I have tried:
.OrderBy("CreateDate desc") and .OrderBy("CreateDate")
.OrderBy("UpdateDate desc") and .OrderBy("UpdateDate")
.OrderBy("Name desc") and .OrderBy("Name")
.OrderBy("Date desc") and .OrderBy("Date") (a property with an alias of "date")
.OrderBy("Heading desc") and .OrderBy("Heading") (a property with an alias of "heading")
The actual value, (but I have stepped through code to look at the value, and it's passed as stated above), are passed in with a PreValue property, DropDown, where I have specifeid the values as described above (camel cased).
Nothing does anything to the resulting list.
This is part of my code: (paste unformatted on a mac is fu**ing impossible)
is working on a reply...