LINQ to Entities only supports casting EDM primitive or enumeration types 错误

LINQ to Entities only supports casting EDM primitive or enumeration types error

SO 上可能有几十个标题重复的问题,如果这是重复的,我深表歉意,但我对 LINQ 的经验有限,我无法找到解决方案。我正在尝试检索最后一条消息,名字和姓氏,以及最后一条消息的发送日期,按 identNumber 分组。 sql 查询工作正常,LINQ returns:

Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types

显然有些东西是空的,但我不确定正确的翻译是什么。

SQL:

SELECT     e.identNumber, e.DateSent, e.FirstName, e.LastName
FROM         Echo.vw_EchoChatMessages AS e,
  (SELECT MAX(DateSent) AS MaxDate, identNumber
  FROM Echo.vw_EchoChatMessages
  GROUP BY identNumber ) MaxResults
WHERE e.identNumber = MaxResults.identNumber
AND e.DateSent = MaxResults.MaxDate

LINQ(我能得到的最接近的):

                var qry = from c in db.vw_EchoChatMessages
                      let maxDate = (from d in db.vw_EchoChatMessages select d.DateSent).Max()
                      where c.DateSent.Equals(maxDate)
                      select new { c.FirstName, c.LastName, c.DateSent };

请多多指教,谢谢!

EF 无法识别编译器为 .Equals() 调用生成的从 Nullable<T>object 的隐式转换。

将其更改为 == 运算符,您应该没问题,因为规范定义了 TNullable<T> 之间的相等运算符。