我担任第一个开发角色已经 6 个月了,并且已经开始在我们的存储库层中使用更多的 LINQ 来查询我们的数据库。对于我创建的两个查询,我真的需要一些帮助。
IQueryable<long> clientsWithoutFeature = from cf in db.Features
where cf.Feature != 9 && cf.Feature == 8
select cf.Client;
IQueryable<long> clientsWithFeature = from cf in db.Features
where cf.Feature == 9 && cf.Feature == 8
select cf.Client;
每个客户端可以有多个功能,每个功能都是一个单独的记录/行。
第一个查询应该返回特征为 8 而不是特征为 9 的所有客户端。但是,它返回所有特征为 8 的客户端,无论客户端是否也具有特征 9。
第二个查询应该返回特征为 8 和特征为 9 的所有客户端。但是,它没有返回任何客户端。
有人可以告诉我我的查询有什么问题吗?
最佳答案
您的 sql 正在按照您编写的方式执行。您需要稍微重组查询以表达您的实际意图。
我倾向于使用像这样的子查询方法:
IQueryable<long> clientsWithoutFeature = from cf in db.Features
where cf.Feature == 8 && !db.Features.Any(x => x.id == cf.id && x.Feature == 9)
select cf.Client;
IQueryable<long> clientsWithFeature = from cf in db.Features
where cf.Feature == 8 && db.Features.Any(x => x.id == cf.id && x.Feature == 9)
select cf.Client;
我不太确定您的主键列是什么。所以我猜是 idY
关于c# - 在 WHERE 子句中具有多个条件的 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41010317/