我将System.Linq.Dynamic用于一个项目,该项目要求用户选择在运行时选择/投影哪些属性。
所以,我有这样的查询:
var query = db.Users.Select("New(UserId, Username, Groups.Max(DateInserted) AS DateInserted)");
DateInserted列不为null,但并非所有用户都有组。因此,当一个没有组的用户返回时,出现以下错误:
"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."
查询中有什么我可以做的吗?我不能使该列可为空。
谢谢你的帮助。
转换为可空类型的DateTime吗?像这样: var query = db.Users.Select("New(UserId, Username, DateTime?(Groups.Max(DateInserted)) AS DateInserted)");