13

In an rdlc report I want to compare integers like

if(expression)
{
     // do something
}
else if(expression)
{
     // do something else
}

What is the syntax for this?

6 Answers 6

46

Rather than using nested IIF statements I prefer the Switch statement.

From the MSDN...

=Switch(
    Fields!PctComplete.Value >= 10, "Green", 
    Fields!PctComplete.Value >= 1, "Blue", 
    Fields!PctComplete.Value = 1, "Yellow", 
    Fields!PctComplete.Value <= 0, "Red"
    )

Hope it helps :)

2
  • Thanks it's work Now i have another problem hope you help me ... I have three type board LIKE:GOOD,REMOVED,NOTFOUND.....i want a report on rdlc that show me Board type amount like below Thana Good Removed NotFound A 5 2 4 B 4 1 0 how can i do that
    – Shamim
    Jun 16, 2009 at 7:42
  • I'm not totally sure what you mean? Can you rephrase the the question in any way, your example has lost its formatting because its a comment.
    – user110714
    Jun 16, 2009 at 7:53
9

You will have to nest IIF statements like this:

 = IIF (expression = 1, "Is 1", IIF (expression = 2, "Is 2"))
2
  • thannks now if i want to compare string then? what i do.... like int Total=0 if(expression=="Good") then TotalIncrease
    – Shamim
    Jun 16, 2009 at 7:24
  • You cant have variables in that sense. You would nest the 'IIF' within a SUM or some other kind of aggregate function... =Sum(IIF(expression = "Good", 1, 0)). Something along those lines.
    – user110714
    Jun 21, 2009 at 16:00
2

Use switch instead. I know I reached late here,but hope that it may help someone.

=Switch(Fields!Parameter.value = 2,"somethingnew", 1=1 ,"somethingelse")

1=1 refers to default in switch case.

It is similar like

if(Parameter.Value == 2)
{
somethingnew
}
else
{
somethingelse
}
1

This is the Syntax for your requirement:

=IIf(CInt(Fields!expression1.value)==1,true,IIf(Cint(Fields!expression2.value)==2,true,nothing))

In true part you can specify the statement to be executed.

1

In addition to what has been said, the If(condition, true_part, false_part) ternary operator should (with one I) be preferred over the IIf(condition, true_part, false_part) function (with two I's) in most cases.

The If() ternary operator will short-circuit evaluate only the part that corresponds to condition outcome (e.g. a condition that is True will only evaluate the true_part).

The IIf() function will always evaluate the condition and both parts, because it is just a function call and all parameters of the function will be evaluated before the call.

Since developers usually expect short-circuit evaluation in IF statements, the usage of the If() ternary operator should be the default choice. It allows you to run expressions that check for Nothing, like the following, which would not work without lazy evaluation:

=If(Fields!Blog.Value IsNot Nothing, Fields!Blog.Value.Name, "Blog is missing")
0

You could also try this example

= IIF(Parameters!expression.Value = True, 'somethingnew', 'somethingelse')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.