3

我将 System.Linq.Dynamic 用于需要用户选择在运行时选择/投影哪些属性的项目。

所以,我有一个这样的查询:

var query = db.Users.Select("New(UserId, Username, Groups.Max(DateInserted) AS DateInserted)");

DateInserted 列不为空,但并非所有用户都有组。因此,当返回没有组的用户时,我收到以下错误:

"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."

我可以在查询中做些什么吗?我不能使该列可以为空。

谢谢你的帮助。

4

1 回答 1

0

转换为可空类型 DateTime?像这样: var query = db.Users.Select("New(UserId, Username, DateTime?(Groups.Max(DateInserted)) AS DateInserted)");

于 2012-11-19T22:06:05.257 回答