Equivalent of SQL ISNULL in LINQ?
Since aa
is the set/object that might be null, can you check aa == null
?
(aa
/ xx
might be interchangeable (a typo in the question); the original question talks about xx
but only defines aa
)
i.e.
select new {
AssetID = x.AssetID,
Status = aa == null ? (bool?)null : aa.Online; // a Nullable<bool>
}
or if you want the default to be false
(not null
):
select new {
AssetID = x.AssetID,
Status = aa == null ? false : aa.Online;
}
Update; in response to the downvote, I've investigated more... the fact is, this is the right approach! Here's an example on Northwind:
using(var ctx = new DataClasses1DataContext())
{
ctx.Log = Console.Out;
var qry = from boss in ctx.Employees
join grunt in ctx.Employees
on boss.EmployeeID equals grunt.ReportsTo into tree
from tmp in tree.DefaultIfEmpty()
select new
{
ID = boss.EmployeeID,
Name = tmp == null ? "" : tmp.FirstName
};
foreach(var row in qry)
{
Console.WriteLine("{0}: {1}", row.ID, row.Name);
}
}
And here's the TSQL - pretty much what we want (it isn't ISNULL
, but it is close enough):
SELECT [t0].[EmployeeID] AS [ID],
(CASE
WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(10),@p0)
ELSE [t2].[FirstName]
END) AS [Name]
FROM [dbo].[Employees] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[FirstName], [t1].[ReportsTo]
FROM [dbo].[Employees] AS [t1]
) AS [t2] ON ([t0].[EmployeeID]) = [t2].[ReportsTo]
-- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
QED?
ISNULL equivalent in Linq
You can use ??
operator instead of ISNULL
:
Table.Where(t=> (t.IsActive ?? false) == act);
How to write SQL IsNull() in Linq?
var InvoiceNo = dbContext.Sales.Max(x => (int?)x.InvoiceNo) ?? 0;
Linq to Sql isnull in where clause not working
just use the coalesce ??
operator
where
(tesrt.EH_PP_TESRT_TeacherEvalStatusID ?? 0) == 1
or if it's a string (not really clear with your code)
(tesrt.EH_PP_TESRT_TeacherEvalStatusID ?? "0") == "1"
But with the sample code given, this seem to be useless (the first version should do the same).
If what you mean is
take all values where tesrt.EH_PP_TESRT_TeacherEvalStatusID IS NULL or
tesrt.EH_PP_TESRT_TeacherEvalStatusID == 1
then
tesrt.EH_PP_TESRT_TeacherEvalStatusID == null || tesrt.EH_PP_TESRT_TeacherEvalStatusID == 1
or
tesrt.EH_PP_TESRT_TeacherEvalStatusID ?? 1 == 1
Using ISNULL on an int? in the WHERE clause of Linq queries
Why not simply:
var q = ctx.Projects.Where(p => p.ParentId == parentId);
That works if parentId
is an int
or int?
.
If you treat the null as 0 this should work:
var q = ctx.Projects.Where(p => (p.ParentId ?? 0) == parentId);
Select IsNull in Linq
You can try the below (have not been able to test, maybe will require some adjustments, but should be in the same form);
from Doc in db.document
join DocDef in db.qryDocumentDefinitions on doc.documentDefId equals DocDef.documentDefId
join Cus in db.Customer on Cus.customerId = Doc.customerId
from loan in db.loan
.Where(loan => loan.loanId == Doc.loanId).DefaultIfEmpty()
select new
{
CustNo= Cus.customerNumber
,loan == null ? "" : loan.loanNumber
,DocType= DocDef.documentTypeName
,DocSubType= DocDef.documentSubTypeName
,filename = Doc.filename
,inputType = (?int)(from Sys in db.SystemProperties
where Sys.propertyKey.Contains("acculoan.inputType.Other")
}
Entity Framework ISNULL in Where condition
Try something like
MyTable.Where( t => (t.Status ?? "") == "CO" )
Linq to Sql IsNull replacing NULL FK with existing FK
Have you tried VB's binary if() operator?
from baseOne in BaseOne _
join m in M on if(baseOne.MChildID,baseOne.MID) equals m.ID _
select baseOne,m
It produces a COALESCE, which is similar to ISNULL.
FROM [BaseOne] AS [t0]
INNER JOIN [M] AS [t1] ON (COALESCE([t0].[MChildID],[t0].[MID])) = [t1].[ID]
Related Topics
No Console Output When Using Allocconsole and Target Architecture X86
Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#
Get Value of a Public Static Field via Reflection
Sqlbulkcopy Insert with Identity Column
What Happens If I Don't Call Dispose on the Pen Object
Attach Debugger in C# to Another Process
How to Disable a System Device Programmatically
How Should Anonymous Types Be Used in C#
C#: Prepending to Beginning of a File
How to Initialize a C# Dictionary with Values
Differencebetween Linq to Xml Descendants and Elements
Detecting Network Connection Speed and Bandwidth Usage in C#
Is Endinvoke() Optional, Sort-Of Optional, or Definitely Not Optional