We are migrating some code to use Entity Framework and have a query that is trying to sort on a Nullable field and provides a default sort value is the value is null using the Nullable.GetValueOrDefault(T) function.
However, upon execution it returns the following error:
LINQ to Entities does not recognize the method 'Int32 GetValueOrDefault(Int32)' method, and this method cannot be translated into a store expression.
The query looks like:
int magicDefaultSortValue = 250;
var query = context.MyTable.OrderBy(t => t.MyNullableSortColumn
.GetValueOrDefault(magicDefaultSortValue));
From this answer I can see that there is a way to provide "translations" within your EDMX. Could we write a similar translation for this coalescing function?
NOTE: When I tried, the ??
coalescing operator instead of GetValueOrDefault in the query it does work. So perhaps whatever makes that work could be leveraged?