我正在为报表服务器创建报表,并且由于缺少用于定义完整方法的代码编辑器*,因此用于设置属性的任何复杂表达式都会非常快速,因为它们必须是单个语句。通常这意味着一个多层嵌套的Iif(...)
炸弹,它以六个关闭括号结束。任意虚构的例子:
=Iif(Fields!myField.Value == "Cancelled",
"It's been cancelled",
Iif(Fields!anotherField.Value < 15,
"Less than 15 things",
Iif(Fields!anotherField.Value >= 15 And Fields!anotherField.Value < 30,
"Between 15 and 30 things",
Iif(Fields!anotherField.Value >=30,
"Over 30 things",
"")))) 'keep hitting that close bracket key until things stop getting red squiggles
呸
我可以做些什么来在我的表达式编辑器中获得一个漂亮的完整方法体?我知道在C#中至少有一些方法可以使用现有方法的名称,或者只是让你直接在那里编写方法,但我不知道那是什么。这似乎就像我想要的那样 - 一个带有方法体“里面”的单个表达式。我希望能够写出类似的表达式:(为我狡猾的VB.Net语法道歉)
= SomeMagic({
If (Fields!myField.Value == "Cancelled") Then
Return "It's been cancelled"
End If
If (Fields!anotherField.Value < 15) Then
Return "Less than 15 things"
End If
If (Fields!anotherField.Value >= 15
And Fields!anotherField.Value < 30) Then
Return "Between 15 and 30 things"
End If
If (Fields!anotherField.Value ?= 45) Then
Return "Over 30 things"
End If
})
如果它可行,但仅限于C#,并且可以将报告使用的语言更改为C#,我愿意接受 - 我对C#比VB.Net更舒服。如果有一种方法可以将主管 *代码编辑器与Report Server一起使用,并在那里定义可以在表达式编辑器中调用(并由其识别)的方法,那么这也是一种可接受的解决方案。
*技术上,是代码编辑器。这是一个对话框。这是明文。它没有编译时错误检查。其中定义的方法无法在表达式编辑器中识别。这几乎没用。
答案 0 :(得分:2)
您可以在 SQL 或 SSRS
中处理它 SSRS方式:使用switch
语句。
= Switch(
Fields!myField.Value = "Cancelled", "It's been cancelled",
Fields!anotherField.Value < 15, "Less than 15 things",
Fields!anotherField.Value >= 15 And Fields!anotherField.Value < 30, "Between 15 and 30 things",
Fields!anotherField.Value>= 30, "Over 30 things"
1=1, "Unknown"
)
在SWITCH
其他地方使用1 = 1条件处理。如果你不想要别的,你可以离开那一部分。
或在您的 SQL查询中处理它,这将更容易维护和调试。
SELECT Col1, Col2, ...
CASE
WHEN myField = 'Cancelled' THEN 'It''s been cancelled'
WHEN anotherField < 15 THEN 'Less than 15 things'
WHEN anotherField BETWEEN 15 AND 30 THEN 'Between 15 and 30 things' --It includes both 15 and 30
WHEN anotherField > 30 THEN 'Over 30 things'
ELSE 'Unknown'
END AS newColumn
FROM ...
WHERE ....