I have an ad-hoc reporting system; I have no compile-time knowledge of the source type of queries or of the required fields. I could write expression trees at runtime using the System.Linq.Expressions.Expression
factory methods, and invoke LINQ methods using reflection, but Dynamic LINQ is a simpler solution.
The reporting system is to allow for queries which returns the result of a LEFT JOIN. There are fields in the joined table which are NOT NULL
in the database; but because this is a LEFT JOIN
, those fields will contain NULL
for certain records. The EF6-generated expression falls on this, because the expression projects to a non-nullable value type.
If I was doing this in compile-time LINQ, I would explicitly cast to the nullable type:
enum Color { Red, Green, Blue }
// using System;
// using static System.Linq.Enumerable;
// using System.Linq;
var range = Range(0, 3).Select(x => (Color)x).AsQueryable();
var qry = range.Select(x => (Color?)x);
Dynamic LINQ supports explicit conversions:
// using static System.Linq.Dynamic.Core
var qry1 = range.Select("int?(it)");
but only a specific set of types can be referenced in the query. If I try to use Color
in the query:
var qry2 = range.Select("Color(it)");
I get the following error:
No applicable method 'Color' exists in type 'Color'
and if I try to explicitly cast to Color?
:
var qry3 = range.Select("Color?(it)");
I get:
Requested value 'Color' was not found.
How can I do this using the Dynamic LINQ library?