Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WHERE子句中具有多个条件的LINQ查询_C#_Mysql_Linq - Fatal编程技术网

C# WHERE子句中具有多个条件的LINQ查询

C# WHERE子句中具有多个条件的LINQ查询,c#,mysql,linq,C#,Mysql,Linq,我担任第一个开发角色已经6个月了,并且已经开始在我们的存储库层中使用更多LINQ来查询数据库。对于我创建的两个查询,我确实需要一些帮助 IQueryable<long> clientsWithoutFeature = from cf in db.Features where cf.Feature != 9 && cf.Feature == 8 select cf.Client; IQueryable&l

我担任第一个开发角色已经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;
IQueryable clientsWithoutFeature=来自db.Features中的cf
其中cf.功能!=9&&cf.功能==8
选择cf.客户机;
IQueryable clientsWithFeature=来自db.Features中的cf
其中cf.Feature==9&&cf.Feature==8
选择cf.客户机;
每个客户端可以有多个功能,每个功能都是一个单独的记录/行

第一个查询应该返回特性为8但特性为9的所有客户机。但是,无论客户机是否具有9的特性,它都会返回所有具有8特性的客户机

第二个查询应该返回特性为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;
IQueryable clientsWithoutFeature=来自db.Features中的cf
其中cf.Feature==8&&!db.Features.Any(x=>x.id==cf.id&&x.Features==9)
选择cf.客户机;
IQueryable clientsWithFeature=来自db.Features中的cf
其中cf.Feature==8&&db.Features.Any(x=>x.id==cf.id&&x.Feature==9)
选择cf.客户机;

我不确定你的主键栏是什么。所以我只是猜测是idY,您的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;
IQueryable clientsWithoutFeature=来自db.Features中的cf
其中cf.Feature==8&&!db.Features.Any(x=>x.id==cf.id&&x.Features==9)
选择cf.客户机;
IQueryable clientsWithFeature=来自db.Features中的cf
其中cf.Feature==8&&db.Features.Any(x=>x.id==cf.id&&x.Feature==9)
选择cf.客户机;

我不确定你的主键栏是什么。所以我猜是田园诗,很简单<代码>特性是一个表,其中单个行不能同时具有
特性
8
9

要获得您想要的,您需要
加入

IQueryable<long> clientsWithoutFeature = from cf1 in db.Features
join cf2 in db.Features on new { cf1.Client, IsTargetFeature = true }
    equals new { cf2.Client, IsTargetFeature = cf2.Feature = 8 }
where cf1.Feature != 9
select cf1.Client;

IQueryable<long> clientsWithFeature = from cf1 in db.Features
join cf2 in db.Features on new { cf1.Client, IsTargetFeature = true }
    equals new { cf2.Client, IsTargetFeature = cf2.Feature = 8 }
where cf1.Feature == 9
select cf1.Client;
IQueryable clientsWithoutFeature=来自db.Features中的cf1
在新{cf1.Client,IsTargetFeature=true}上的db.Features中加入cf2
等于new{cf2.Client,IsTargetFeature=cf2.Feature=8}
其中cf1.Feature!=9
选择cf1.客户端;
IQueryable clientsWithFeature=来自数据库Features中的cf1
在新{cf1.Client,IsTargetFeature=true}上的db.Features中加入cf2
等于new{cf2.Client,IsTargetFeature=cf2.Feature=8}
其中cf1.Feature==9
选择cf1.客户端;

这很简单<代码>特性
是一个表,其中单个行不能同时具有
特性
8
9

要获得您想要的,您需要
加入

IQueryable<long> clientsWithoutFeature = from cf1 in db.Features
join cf2 in db.Features on new { cf1.Client, IsTargetFeature = true }
    equals new { cf2.Client, IsTargetFeature = cf2.Feature = 8 }
where cf1.Feature != 9
select cf1.Client;

IQueryable<long> clientsWithFeature = from cf1 in db.Features
join cf2 in db.Features on new { cf1.Client, IsTargetFeature = true }
    equals new { cf2.Client, IsTargetFeature = cf2.Feature = 8 }
where cf1.Feature == 9
select cf1.Client;
IQueryable clientsWithoutFeature=来自db.Features中的cf1
在新{cf1.Client,IsTargetFeature=true}上的db.Features中加入cf2
等于new{cf2.Client,IsTargetFeature=cf2.Feature=8}
其中cf1.Feature!=9
选择cf1.客户端;
IQueryable clientsWithFeature=来自数据库Features中的cf1
在新{cf1.Client,IsTargetFeature=true}上的db.Features中加入cf2
等于new{cf2.Client,IsTargetFeature=cf2.Feature=8}
其中cf1.Feature==9
选择cf1.客户端;

where条件独立应用于每一行时,同一行/记录是否有两个具有多个值的“功能”列。我认为你的质疑是不对的。这不是Linq的问题。我不熟悉Linq,但您的查询应该构造为一个嵌套查询,其中“not in”clauseConsider显示您的模型。where条件独立应用于每一行,同一行/记录是否有两个具有多个值的“Feature”列。我认为你的质疑是不对的。这不是Linq的问题。我不熟悉Linq,但是您的查询应该被构造为一个嵌套查询,其中“notin”clauseConsider显示您的模型。这是一个很好的答案,也是我最终用来解决问题的答案。谢谢@meganautt这是一个很好的答案,也是我最终用来解决问题的答案。谢谢你@meganaut