Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of SQL ISNULL in LINQ?

Tags:

c#

sql

linq

isnull

In SQL you can run a ISNULL(null,'') how would you do this in a linq query?

I have a join in this query:

var hht = from x in db.HandheldAssets         join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo         from aa in DevInfo.DefaultIfEmpty()         select new         {         AssetID = x.AssetID,         Status = xx.Online         }; 

but I have a column that has a bit type that is non nullable (xx.online) how can I set this to false if it is null?

like image 514
MartGriff Avatar asked Jan 05 '09 13:01

MartGriff


People also ask

Is NULL SQL in Linq?

NULL in SQL means, "value absent, will match any comparison", whereas null in . NET means "no object, comparing against null will always yield false".

Is NULL in Linq C#?

LINQ to SQL does not impose C# null or Visual Basic nothing comparison semantics on SQL. Comparison operators are syntactically translated to their SQL equivalents. The semantics reflect SQL semantics as defined by server or connection settings.


2 Answers

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?

like image 60
Marc Gravell Avatar answered Sep 20 '22 16:09

Marc Gravell


You can use the ?? operator to set the default value but first you must set the Nullable property to true in your dbml file in the required field (xx.Online)

var hht = from x in db.HandheldAssets         join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo         from aa in DevInfo.DefaultIfEmpty()         select new         {         AssetID = x.AssetID,         Status = xx.Online ?? false         }; 
like image 22
bruno conde Avatar answered Sep 19 '22 16:09

bruno conde