我正在暑期编程实习的第一周,并负责使用LINQPad将SQL Server 2008语句转换为C#。一旦我这样做并使其正常工作,我可以将其输入VS并根据需要进行调整。
问题是我正在运行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语句:
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;
}
我很感激有人可以指出我正确的方向。
答案 0 :(得分:2)
这样的事情:
Hours = condition ? code when true : code when false;