Thanks. However, this does not give the result that I need. The idea is this:
The property DateCreated always has a value while DateProcessed may be null in some cases.
Now, I want to genreally sort by DateProcessed desc but in case the field is null, it should use DateCreated as fallback value for the sorting order. With v5 this was possible using the coalescing operator.
Thanks for the clarification! We will research on how to restore this functionality and I'll update this thread when ready. The fix for more than two hierarchy levels of sub properties in Select() predicate was just published.
Thanks a lot! Maybe the LINQ ThenByDescending could be a way to implement that. However, the coalescing operator would be the best option since it also can be used for other things in this context.
The fix for more than two hierarchy levels of sub properties in Select() predicate works fine! However, one should note that in case of sub properties, it is required to add a "as", so
Contact.Name
will give an exception, while
Contact.Name as ContactName
works as expected. Simple properties do not require that, so you can both use
Well, if I use the syntax above, I get this exception:
System.ArgumentException: 'x' is not a member of type 'MyApp.Models.MyDataContext.Purchase' (Parameter 'propertyOrFieldName')
at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
at Radzen.QueryableExtension.GetNestedPropertyExpression(Expression expression, String property, Type type)
However, if I change it to:
$"DateProcessed ?? DateCreated desc"
I do not get an exception, but it gives the same results as
$"DateProcessed desc"
so the other property is ignored.
As a result, the data is sorted descending by DateProcessed and all records, where DateProcessed is NULL are shown at the end of the resultset. The correct behaviour would be that for those records with DateProcessed == NULL the value of DateCreated is used instead to determine the position in the sort order.
Very strange. I'm not using this with the export controller but even if I leave the filter expression blank and only use the orderby expression I get an error as soon as I use the x => x. syntax. If I only supply the property names, it works but only sorts after the first property and ignores the ?? and the second property...
I selected "Region" and "Country" for this example, because every record has a Country but not necessarily a Region, so if Region is NULL, it should use Country as fallback for the sorting order.
When you run the code and open the page, you get this Exception:
System.ArgumentException: 'i' is not a member of type 'Northwind.Models.NorthwindData.Customer' (Parameter 'propertyOrFieldName')
at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
at Radzen.QueryableExtension.GetNestedPropertyExpression(Expression expression, String property, Type type)
at Radzen.QueryableExtension.OrderBy(IQueryable source, String ordering)
at Radzen.QueryableExtension.OrderBy[T](IQueryable`1 source, String ordering)
Thanks. In the Northwind sample, there is no exception any more but the result is not as expected. It sorts by Region descending, but all the records that do not have a region (Region == NULL) are at the end of the grid (which would be correct if I would have only sorted by Region desc).
But the "Country" should be used to determine the sort order for those records, that do not have a Region.
Region ?? Country desc
is supposed to do this (logically):
Put all records that have a Region into a new, "virtual" dataset with an extra column "Ordering" that contains the values of the "Region" property. Next, add all records that do not have a Region (Region == NULL) to that dataset as well and use the values of "Country" to fill the "Ordering" column for those. Finally, do an OrderBy Desc on the "Ordering" column. Of course this done differently internally, but it illustrates what the expected output should be.
In addition to that, in my app where I used two DateTime properties for the ordering, I now do not get a different exception:
Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer: Warning: Unhandled exception rendering component: Index and length must refer to a location within the string. (Parameter 'length')
Confirmed. The exception is gone now, also in my app. Thanks!
However, the intial problem remains meaning that the coalescing operator does not work as espected.
DateProcessed ?? DateCreated desc
This only sorts by DateProcessed and completely ignores DateCreated. In the example that you posted in this thread, you used two sorting properties of different data types. Better try with columns of the same type.
You can e.g. use the Northwind DB as example using the "Customers" page with this code:
It will be sorted by Region desc but all records that do not have a Region are at the end which would be correct for only sorting by Region desc. But with ?? Country it should pick Country for those records, that do not have a Region in order to determine the sorting property.
Can you confirm this?
Update: When you look at the SQL in the debug output of VS, you can see that the expression gets evaludated to: ORDER BY COALESCE([p].[DateProcessed], NULL) DESC - however, it should be ORDER BY COALESCE([p].[DateProcessed], [p].[DateCreated]) DESC
So it looks to me if the OrderBy expression is not correctly translated into SQL...
Looks like there is a regression to this issue. Until version 6.5.2 this was working fine for now. But since version 6.5.3 up to the latest version 6.6.0, I once again get a "System.ArgumentException" stating:
System.ArgumentException: 'NextBillingDate ?? FirstBillingDate' is not a member of type 'MyModel.Subscription' (Parameter 'propertyOrFieldName')
at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
at Radzen.QueryableExtension.GetNestedPropertyExpression(Expression expression, String property, Type type)
at Radzen.QueryableExtension.OrderBy(IQueryable source, String selector)
at Radzen.QueryableExtension.OrderBy[T](IQueryable`1 source, String selector)
Seems this got messed up again. It would be nice if you could check what the actual problem is here and provide a fix. Thanks a lot!