English 中文(简体)
将语句转换为 C# 时 SQL 案例转换为 C#
原标题:Converting SQL CASE WHEN statement into C#

我在暑期方案实习的第一周,负责使用 rel=“nofollow”>LINQPad ,将SQL Server 2008 声明转换为C#。一旦我这样做并正常工作,我就可以根据需要将其输入VS和tweak。

问题是,我正在查看 SQL 语句,我不知道如何转换。在我的 C# 类中,我们没有将 SQL 语句转换为 C#, 我们刚刚创建了一个字符串变量, 将 SQL 语句指定为变量, 然后将变量指定为 OleDbCommand 对象。 我的导师花了一整天的周末休假, 我几乎独自一个人, 不清楚该怎么做和需要一些帮助。 有几十个类似的例子, 如果我能找到如何做的话, 我可以找出其余的。 以下是 SQL 语句 :

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

我敢肯定,这是一个如果声明 大致如下:

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]

我尝试了以下的代码,我认为它不会工作,但需要从某个地方开始:

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;
    }   

如果有人能指示我正确方向,我将不胜感激。

最佳回答

像这样的事情:

Hours = condition ? code when true : code when false;
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签