加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq to Entities“无法识别方法…方法,并且此方法无法转

发布时间:2020-12-16 01:58:16 所属栏目:百科 来源:网络整理
导读:参见英文答案 Method cannot be translated into a store expression????????????????????????????????????1个 我有个问题.我在Visual Studio中创建了一个数据实体模型.在林克; Guid RtuDataId = db.RtuData.Where(x = x.CommunicationUnit.Id == new Guid(I
参见英文答案 > Method cannot be translated into a store expression????????????????????????????????????1个
我有个问题.我在Visual Studio中创建了一个数据实体模型.在林克;

Guid RtuDataId = db.RtuData.Where(x => x.CommunicationUnit.Id == new Guid(ID))
    .OrderByDescending(x => x.ReadOn)
    .LastOrDefault().Id;

我得到错误;

does not recognize the method … method,and this method cannot be translated into a store expression.

我在谷歌搜索但我不明白这个错误.

解决方法

它的LastOrDefault在LINQ to Entites中不受支持.我不确定您使用OrderByDescending的原因,而是可以使用OrderBy然后选择First.如果你在查询之前创建新的Guid然后将它传递给你的where子句,它会更好:

var guid = new Guid(ID);
Guid RtuDataId = db.RtuData
                   .Where(x => x.CommunicationUnit.Id == guid)
                   .OrderBy(x => x.ReadOn)
                   .FirstOrDefault()
                   .Id;

由于FirstOrDefault可能返回null,因此您应该在访问Id之前检查null

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读