Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to convert the below sql statement to linq. But how do i get the ISNULL() area in my SQL query?

SQL
SELECT DISTINCT lkpChecks.txtChecksName AS [Check Name],
    lkpChecks.intTimeInterval AS [Time Interval],
    lkpChecks.intBatchInterval AS [Batch Interval],
    lkpChecks.ynStartUp AS [Start Up],
    lkpChecks.ynCCP AS [CCP],
    lkpChecks.ynBoolean AS [Type Ok/Not Ok],
    lkpChecks.txtUnit AS [Unit],
    ISNULL(tblChecks.dblMinValue, ISNULL(lkpChecks.dblDefaultMinValue, 0)) AS [Minimum Value],
    ISNULL(tblChecks.dblMaxValue, ISNULL(lkpChecks.dblDefaultMaxValue, 0)) AS [Maximum Value],
    ISNULL(tblChecks.dblLowerWarningLimit, ISNULL(lkpChecks.dblDefaultMinWarningValue, 0)) AS [Lower Warning Value],
    ISNULL(tblChecks.dblUpperWarningLimit, ISNULL(lkpChecks.dblDefaultMaxWarningValue, 0)) AS [Upper Warning Value],
    ISNULL(tblChecks.dblTarget, ISNULL(lkpChecks.dblDefaultTargetValue, 0)) AS [Target],
    lkpChecks.ynEndFillingCode AS [Get at End of LotCode],
    lkpChecks.ynFRReport_Other AS [Include in Release Report],
    lkpChecks.intChecks,
    lkpChecks.intUserEvent,
    ynReqRemarks as [Required Remarks]
FROM lkpChecks LEFT OUTER JOIN tblChecks ON lkpChecks.intChecks = tblChecks.intChecks 
	AND tblChecks.intProcess = lkpChecks.intProcess
	AND tblChecks.intLine = 167
WHERE (lkpChecks.intProcess = 10)


C#
var qs = from check in checksColl
	join check_sett in checkSetting
        on new
        {
            cID = check.ChecksID,
            cProcess = check.Process
        }
        equals
        new
        {
            cID = check_sett.CheckID,
            cProcess = check_sett.ProcessID
        } into temp
          from x in temp.Where(line => line.LineID == 10).DefaultIfEmpty()
where check.Process == int.Parse(c_cboProcess.Items[c_cboProcess.SelectedIndex].Key)
select new { CheckName = check.ChecksName, 
		TimeInterval = check.TimeInterval, 
		BatchInterval = check.BatchInterval, 
		Unit = check.Unit 
	   };
The above linq is ok, but when i add some fields from "x" i'm getting this error "Object is not set to an instance of an object".

C#
select new { CheckName = check.ChecksName, 
		TimeInterval = check.TimeInterval, 
		BatchInterval = check.BatchInterval, 
		Unit = check.Unit,
		MinimumValue = x.MinValue,
		MaximumValue = x.MaxValue	   };


Appreciate your help.

Thanks!!
Posted

Add code to change if x is null ? That's what the SQL is doing.
 
Share this answer
 
Hi Christian,

Thanks for your inputs.

How do i fix the "Object is not set to an instance of an object" error message?

I cannot get the value from the "x".

Thanks
 
Share this answer
 
Comments
Christian Graus 10-Aug-11 22:41pm    
Please don't push 'answer' to ask questions. One obvious solution is to change your data so there's no null values for x being returned. http://www.brentlamborn.com/post/LINQ-to-SQL-Null-check-in-Where-Clause.aspx has some info on checking for null in LINQ with SQL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900