I am using System.Linq.Dynamic for a project that requires the user to choose which properties will be selected/projected at runtime.
So, I have a query like this:
var query = db.Users.Select("New(UserId, Username, Groups.Max(DateInserted) AS DateInserted)");
The DateInserted column is not null, but not all users have groups. So, when a user is returned without groups, I get the following error:
"The cast to value type 'DateTime' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."
Is there anything I can do in the query? I cannot make the column nullable.
Thanks for any help.
Convert to nullable type DateTime? like this:
var query = db.Users.Select("New(UserId, Username, DateTime?(Groups.Max(DateInserted)) AS DateInserted)");