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?

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
Shamim
  • 359
  • 4
  • 12
  • 25

6 Answers6

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 :)

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • 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 '09 at 07: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. –  Jun 16 '09 at 07:53
9

You will have to nest IIF statements like this:

 = IIF (expression = 1, "Is 1", IIF (expression = 2, "Is 2"))
Ben Martin
  • 750
  • 3
  • 6
  • thannks now if i want to compare string then? what i do.... like int Total=0 if(expression=="Good") then TotalIncrease – Shamim Jun 16 '09 at 07: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. –  Jun 21 '09 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
}
Saeed ur Rehman
  • 727
  • 2
  • 10
  • 25
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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
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")
lauxjpn
  • 4,749
  • 1
  • 20
  • 40
0

You could also try this example

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

Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40