Convert SQL CASE WHEN statement in C #

I am in my first week of a summer internship in programming and have been tasked with converting SQL Server 2008 statements to C # using LINQPad . As soon as I do this and work correctly, I can enter it in VS and configure it as needed.

The problem is that I am executing SQL queries, I have no idea how to convert. In my C # class, we did not convert the SQL statements to C #, we just created a string variable, assigned an SQL statement to a variable, and then assigned the variable to the OleDbCommand object. My mentor took the day off for a long weekend, and to a large extent I myself do not know what to do and need help. There are a few dozen, like this one, and if I can see how to do this, I can understand the rest. Here is the SQL statement:

,[hrs].{Hours] - SUM(
  CASE 
    WHEN [UnitState].[UnitStateTye] <> 'ACTIVE' 
      THEN [Allocation].[AllocatedEnergyMwh] 
    ELSE 0 
  END 
/ CAST([Unit].[NetDependableCapacity] AS FLOAT)) AS SH

I am sure this is an if statement line by line:

if [UnitState].[UnitStateType] does not equal active 
then SH equals [hrs].[Hours] minus the the sum of
  [Allocation].[AllocatedEnergyMwh] / (float)[Unit].[NetDependableCapacity].  
else 
  SH = [hrs].[Hours]

I tried the following code, which I thought was not going to work, but it needed to start somewhere:

var results = 

    (from v in VDimUnit     
        join vf in VFactEnergyAllocation on v.UnitKey equals vf.UnitKey
        join vd in VDimGadsEvent on vf.GadsEventKey equals vd.GadsEventKey
        join vt in VDimTime on vf.TimeKey equals vt.TimeKey 
        join vus in VDimUnitState on vf.UnitKey equals vus.UnitKey
    where vd.GadsEventEndTime.Year == 2011 && v.UnitId == "THL3"
    group vf by new {vus.UnitStateType, vf.AllocatedEnergyMwh v.NetDependableCapacity,
        vt.QuarterNum} into groupItem       
    select new {groupItem.Key.QuarterNum, SUM1 = groupItem.Sum(x=> (float)x.AllocatedEnergyMwh /
        groupItem.Key.NetDependableCapacity)}).ToArray();

var resultHours = 
    (from v in VDimTime
    group v by v.QuarterNum into groupItem
    select new {Hours = groupItem.Count(), groupItem.Key}).ToDictionary(x=> x.Key, x=> x.Hours);

var finalResults = 
    (from r in results
    select new {SH_IF_NOT_ACTIVE = resultHours[r.QuarterNum] - r.SUM1,
        Hours = 
        SH_IF_ACTIVE = resultHours[r.QuarterNum]});

    if (SH != "ACTIVE")
    { 
        SH_IF_NOT_ACTIVE;
    }
    else
    {
        SH_IF_ACTIVE;
    }   

I would appreciate it if someone could point me in the right direction.

+5
source share
1

- :

Hours = condition ? code when true : code when false;
+2

All Articles