Print Page | Close Window

Select case in LINQ

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4644
Printed Date: 05-May-2024 at 9:04pm


Topic: Select case in LINQ
Posted By: BillG
Subject: Select case in LINQ
Date Posted: 04-Feb-2014 at 6:53am
How would the be done in LINQ objects

  tSQL = "SELECT CASE                                                     " & _
                 "       WHEN PerCapRate is null                                  " & _
                 "       THEN 0                                                   " & _
                 "       ELSE PerCapRate                                          " & _
                 "       END                         AS PerCapRate                " & _
                 "     , CASE                                                     " & _
                 "       WHEN WageMultiplier is null                              " & _
                 "         OR WageMultiplier = 0                                  " & _
                 "       THEN 1                                                   " & _
                 "       ELSE WageMultiplier                                      " & _
                 "       END                         AS WageMultiplier            " & _
                 "  FROM TradeRates                                      



Replies:
Posted By: smi-mark
Date Posted: 04-Feb-2014 at 10:13am
I would personally just bring them back as is from the table and not worry about trying to do that in SQL. Mapped something like decimal? or rate? (assuming C#) and then do a null check in the code. You could do something like


public int Rate { get { return _rate ?? 0; } }



Posted By: Engo
Date Posted: 05-Feb-2014 at 12:18am
from i in Invoices
select i.Description ?? "NullValue"

Translates to

SELECT 
CASE WHEN Extent1.Description IS NULL THEN 'NullValue' ELSE Extent1.Description END AS C1
FROM Invoice AS Extent1



Print Page | Close Window