Dynamic Linq .Select() - How to handle certain null values

dynamic-linq linq-to-entities

Question

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.

1
3
7/9/2012 7:50:35 PM

Popular Answer

Convert to nullable type DateTime? like this: var query = db.Users.Select("New(UserId, Username, DateTime?(Groups.Max(DateInserted)) AS DateInserted)");

0
11/20/2012 4:17:01 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow