New Posts New Posts RSS Feed: Select case in LINQ
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Select case in LINQ

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: Select case in LINQ
    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                                      
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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; } }



Edited by smi-mark - 04-Feb-2014 at 10:13am
Back to Top
Engo View Drop Down
Newbie
Newbie


Joined: 20-Sep-2011
Posts: 6
Post Options Post Options   Quote Engo Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down