The latter doesn't work. Is there some limitation on OrderBy that prevents me from using strongly typed syntax? I just want to pull in the variable :-S
Just to add, when sticking break points I can see that .OrderBy(x => x.GetPropertyValue(sortbyoption) brings back the correct information, the Linq statement just seems to ignore it.
When I use this I can see, using breakpoints in VS that it's pulling in the correct value (e.g. Name). But the query doesn't seem register this. When I hardcode it there is no issue and it works so...
As mentioned in post #1, if I use dynamics it works as well. So it appears for some reason LINQ doesn't like me dropping in a property value into the query, even though my syntax appears to be fine?
sortbyoption is derived from a switch, so contains a string of "Name".
This, as I've just found appears to be the issue as LINQ’s OrderBy() method doesn’t accept string parameters, only lambda expressions apparently. That'll explain why everything I'm trying fails!
The code is a LeBlender grid editor that allows editors to pick a folder/s and then select ordering/sorting on them and drop them into a page.
So "Name" starts life in the LeBlender editor as "File Name", it's then switched to be a string of "Name"; at this point it's still just a string variable.
I then want to take that and integrate it into my LINQ query so it will sort by the property x.Name, so I'm trying to create the LINQ query based upon a few variables in the code block. At no point am I looking for an Umbraco Node Name; I'm just taking a string variable (with a value of "Name") and trying to drop it in mid-LINQ query to create x.Name.
Hopefully that makes a little more sense now. Thanks for your continued help Alex.
Has anyone successfully used Dynamic LINQ code like that shown here on SO within an Umbraco environment or indeed System.Linq.Dynamic (nuget link: https://www.nuget.org/packages/System.Linq.Dynamic/) ? And will it work within Umbraco 8?
Ok for those having the same issue, the solution that appears to be working for me is to create the orderby statement within a function, then call the function in.
Using LINQ Orderbys with string parameters
Hi all,
I've a query like so:
This works, but the call in the OrderBy to a variable is dynamic; which I want to avoid so I was trying....
The latter doesn't work. Is there some limitation on OrderBy that prevents me from using strongly typed syntax? I just want to pull in the variable :-S
Any help appreciated as usual folks!
EDIT: Umb 7.10.4
Just to add, when sticking break points I can see that .OrderBy(x => x.GetPropertyValue(sortbyoption) brings back the correct information, the Linq statement just seems to ignore it.
No one have any ideas on this one?
Hi Paul
I think the issue is in a type of property value, please, provide a type of property value, "string" for example:
GetPropertyValue() - returns an object type, that is hard to compare and sort
Thanks,
Alex
Hi Paul
Did you solve the issue?
Alex
Hi Alex,
Unfortunately not; It appears my issue is with the line:
When I use this I can see, using breakpoints in VS that it's pulling in the correct value (e.g. Name). But the query doesn't seem register this. When I hardcode it there is no issue and it works so...
This works:
But this does not:
If I pull out the value for sortbyoption on the same page it returns "Name" which is correct. I'm very puzzled by why this isn't working.
As mentioned in post #1, if I use dynamics it works as well. So it appears for some reason LINQ doesn't like me dropping in a property value into the query, even though my syntax appears to be fine?
What is "sortbyoption" in this line?
sortbyoption is derived from a switch, so contains a string of "Name".
This, as I've just found appears to be the issue as LINQ’s OrderBy() method doesn’t accept string parameters, only lambda expressions apparently. That'll explain why everything I'm trying fails!
There is no "Name" property in Umbraco Nodes, it's reserved property that you can call directly like x.Name
Looks like I've not made myself very clear here !
The code is a LeBlender grid editor that allows editors to pick a folder/s and then select ordering/sorting on them and drop them into a page.
So "Name" starts life in the LeBlender editor as "File Name", it's then switched to be a string of "Name"; at this point it's still just a string variable.
I then want to take that and integrate it into my LINQ query so it will sort by the property x.Name, so I'm trying to create the LINQ query based upon a few variables in the code block. At no point am I looking for an Umbraco Node Name; I'm just taking a string variable (with a value of "Name") and trying to drop it in mid-LINQ query to create x.Name.
Hopefully that makes a little more sense now. Thanks for your continued help Alex.
Has anyone successfully used Dynamic LINQ code like that shown here on SO within an Umbraco environment or indeed System.Linq.Dynamic (nuget link: https://www.nuget.org/packages/System.Linq.Dynamic/) ? And will it work within Umbraco 8?
https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet
I can see this question coming up a lot once Umb8 drops dynamics and we're all using strongly typed syntax.
Thread updated slightly. As the rewrite of this code is so it'll still work when dynamics are dropped in Umb 8, I've marked it as an Umbraco 8 query.
Ok for those having the same issue, the solution that appears to be working for me is to create the orderby statement within a function, then call the function in.
E.g.
So effectively I'm using a function as a variable.
Hopefully this will help someone down the line!
is working on a reply...