I have an SQL query that I’d like to convert to a LINQ expression but have an issue with a nullable bool.
SQL
SELECT j.Company, j.JobNum, j.AssemblySeq, j.PartNum, j.Description, j.IUM
, j.RequiredQty, j.IssuedComplete, j.RelatedOperation, j.IssuedQty
, j.WarehouseCode, j.MtlSeq, p.QtyBearing
, (j.RequiredQty - j.IssuedQty) AS Shortage
FROM Erp.JobMtl as j
Left OUTER JOIN Erp.PartPlant p
ON j.Company = p.Company AND j.PartNum = p.PartNum
WHERE j.Company='HLI' and j.JobNum='TEST2' and j.AssemblySeq= 0
and (p.QtyBearing = 1 or p.QtyBearing IS NULL)
and (j.IssuedComplete = 0)
LINQ
var OutStandMtl = (
from jm in Db.JobMtl
join pp in Db.PartPlant
on new { jm.Company, jm.PartNum } equals new { pp.Company, pp.PartNum }
where jm.Company == "HLI" && jm.JobNum == "TEST2" && jm.AssemblySeq == 0
&& (pp.QtyBearing == true || pp.QtyBearing == null)
&& jm.IssuedComplete == false
select new { jm.Company, jm.JobNum, jm.AssemblySeq, jm.PartNum, jm.Description
, jm.IUM, jm.RequiredQty, jm.IssuedComplete, jm.RelatedOperation
, jm.IssuedQty, jm.WarehouseCode, jm.MtlSeq
, Shortage = (jm.RequiredQty - jm.IssuedQty) });
Error
CS0472: The result of the expression is always ‘false’ since the value of type ‘bool’ is never equal to ‘null’ of type ‘bool’
This query gives me a list of all outstanding mtl on a job, except parts on the fly because they don’t have PartPlant records… In SQL I can use pp.QtyBearing IS NULL, which gives me the correct results. However, in C# LINQ it seems to knows that pp.QtyBearing is a non-nullable bool, which is why I get that message.
Very nice, I recently ran into a similar issue as Norman where POTF bit me because the part is --of course-- not in the Part table. It was a Fill Table by Query BPM node, I was using the expression for a UD checkbox field to check the Part table and set the checkbox accordingly. So it’d need to be a single expression statement. Ended up making a C# node for it to check for the null.
could be that if you swapped the condition’s order around it might work… checking for the null first, becuase if you check for the “true” first, but it is null, it may give you an error, but checking for the null first, it wont try the true condition UNLESS it is not null.