gpt4 book ai didi

c# - Linq 查询中的“IN”和 'NOT IN'

转载 作者:太空狗 更新时间:2023-10-29 21:25:02 32 4
gpt4 key购买 nike

我已经在 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))

对您的原始查询执行相同的操作。

关于c# - Linq 查询中的“IN”和 'NOT IN',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4301023/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com