在 Linq 中使用布尔数据类型检查 where 子句中的两个条件时列表为空
List is null when checking for two conditions in where clause with boolean datatype in Linq
在 linq 中,我遇到了一个奇怪的问题。每当我尝试在 where
子句中使用 bit datatype
指定两列时,它都会返回 null。但问题是在我的 table.
中存在具有该特定要求的数据
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where ( PD.FILEFLAG == false) && (IMG.IsVerify == true )
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 0 even though data exists in table.
相同的查询,如果我以 SQL 查询的形式编写,然后工作并返回一行。
select * from KYC_PERSONAL_DETAILS, KYC_UPLOAD_DETAILS
where KYC_PERSONAL_DETAILS.FILEFLAG = false
AND KYC_UPLOAD_DETAILS.IS_VERIFY= true
//Here it is returning a row.
我只想知道是否可以在 linq 的 where 子句中使用布尔数据类型和 &&
运算符指定两列?如果是,那么请提出一个方法。
经过反复试验后,我找到了使 LINQ 查询正常工作的方法。虽然我仍然不知道背后的原因,但如果我通过以下任何一种方式进行操作,它现在就可以工作了。在研究了这背后的实际问题和逻辑之后,我会更新我的答案。
如果我编写与
相同的查询
1.
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where PD.FILEFLAG == false
where IMG.IsVerify == true
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 1 data that exists in table.
2.
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where ( PD.FILEFLAG == false) & (IMG.IsVerify == true )
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 1 data that exists in table.
通过在布尔数据类型的情况下编写相同的查询,它运行良好。
我认为使用短路运算符可能会有问题。
So, At the end either using &
instead of &&
or using two where
in LINQ query will work well whenever you want to deal with two
columns with boolean
datatype. Using &&
for other datatypes is alright but in case of boolean it could create problems.
更新 1
作为旁注
Though using 2nd Option is not recommended as stated by Ivan in comments. Because I'm using SQL SERVER 2008 R2
and Ivan claims that using &&
in non sql DB is working fine and it's also possible that I might be facing a bug in SQL Server..
在 linq 中,我遇到了一个奇怪的问题。每当我尝试在 where
子句中使用 bit datatype
指定两列时,它都会返回 null。但问题是在我的 table.
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where ( PD.FILEFLAG == false) && (IMG.IsVerify == true )
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 0 even though data exists in table.
相同的查询,如果我以 SQL 查询的形式编写,然后工作并返回一行。
select * from KYC_PERSONAL_DETAILS, KYC_UPLOAD_DETAILS
where KYC_PERSONAL_DETAILS.FILEFLAG = false
AND KYC_UPLOAD_DETAILS.IS_VERIFY= true
//Here it is returning a row.
我只想知道是否可以在 linq 的 where 子句中使用布尔数据类型和 &&
运算符指定两列?如果是,那么请提出一个方法。
经过反复试验后,我找到了使 LINQ 查询正常工作的方法。虽然我仍然不知道背后的原因,但如果我通过以下任何一种方式进行操作,它现在就可以工作了。在研究了这背后的实际问题和逻辑之后,我会更新我的答案。
如果我编写与
相同的查询1.
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where PD.FILEFLAG == false
where IMG.IsVerify == true
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 1 data that exists in table.
2.
var data = (from PD in C.KYC_PERSONAL_DETAILS
join IMG in C.KYC_UPLOAD_DETAILS on PD.ACCOOUNT_NO equals IMG.ACCOOUNT_NO
where ( PD.FILEFLAG == false) & (IMG.IsVerify == true )
select new
{
PD_ACCOUNT_NO = PD.ACCOOUNT_NO,
PD_NAME = PD.NAME,
IMG_Name = IMG.Name
}.ToList();
int count = data.Count(); //Here it shows 1 data that exists in table.
通过在布尔数据类型的情况下编写相同的查询,它运行良好。 我认为使用短路运算符可能会有问题。
So, At the end either using
&
instead of&&
or using twowhere
in LINQ query will work well whenever you want to deal with two columns withboolean
datatype. Using&&
for other datatypes is alright but in case of boolean it could create problems.
更新 1
作为旁注
Though using 2nd Option is not recommended as stated by Ivan in comments. Because I'm using
SQL SERVER 2008 R2
and Ivan claims that using&&
in non sql DB is working fine and it's also possible that I might be facing a bug in SQL Server..