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

c# – Linq查询中的’IN’和’NOT IN’

发布时间:2020-12-15 08:39:30 所属栏目:百科 来源:网络整理
导读:我在LINQPad中测试了以下查询,它运行正常,但VS2010不喜欢它. var topJobs = from j in streetlightDBEntities.Job let mjobid = from m in streetlightDBEntities.Job.Include("Streetlight") where m.Streetlight.StreetlightId == j.Streetlight.Streetlig
我在LINQPad中测试了以下查询,它运行正常,但VS2010不喜欢它.
var topJobs = from j in streetlightDBEntities.Job
    let mjobid = from m in streetlightDBEntities.Job.Include("Streetlight")
                 where m.Streetlight.StreetlightId == j.Streetlight.StreetlightId
                 orderby m.DateCompleted descending
                 select m.JobId
        where mjobid.Take(5).Contains(j.JobId)
        select j.JobId;

var notTopJobs = streetlightDBEntities.Job.Where(c => !topJobs.Contains(c.JobId));

我收到以下错误:

LINQ to Entities does not recognize the method ‘Boolean Contains[String](System.Linq.IQueryable`1[System.String],System.String)’ method,and this method cannot be translated into a store expression.

解决方法

使用 Queryable.Any<TSource>.

而不是这个:

var notTopJobs = streetlightDBEntities
      .Job
      .Where(c => !topJobs.Contains(c.JobId));

做这个:

var notTopJobs = streetlightDBEntities
      .Job
      .Where(c => !topJobs.Any(x => x.JobId == c.JobId))

对原始查询执行相同操作.

(编辑:李大同)

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

    推荐文章
      热点阅读