slandau
slandau

Reputation: 24052

Trying to do an inline if statement in c# with multiple options

So I have two nullable decimals:

s.SwapProfitAmount
s.SwapProfitBps

Then I have a property that needs to get set to one of the values of one of these decimals called Profit.

What I want is an line if statement that will set Profit to the Value of whichever one of the nullable decimals that HasValue and has a Value greater than 0. If they both are 0, it will just set it to 0. Make sense?

Edit:

Profit

is a string.

Upvotes: 3

Views: 25301

Answers (6)

cwharris
cwharris

Reputation: 18125

This should work. Why do you need this in a single line?

Profit = (s.SwapProfitAmount.HasValue && s.SwapProfitAmount.Value > 0 ? s.SwapProfitAmount.Value : s.SwapProfitBps.GetValueOrDefault(0)).ToString();

And for readability...

Profit = (
  s.SwapProfitAmount.HasValue && s.SwapProfitAmount.Value > 0
    ? s.SwapProfitAmount.Value
    : s.SwapProfitBps.GetValueOrDefault(0)
  ).ToString();

And since you said you were using LINQ, this might apply...

var results = from s in somethings
              let bps = s.SwapProfitBps.GetValueOrDefault(0)
              let amount = s.SwapProfitAmount
              let profit = amount.HasValue && amount.Value > 0
                           ? amount.Value
                           : bps
              select profit.ToString();

These will all fall back to SwapProfitBps when SwapProfitAmount <= 0 or == null

Finally, like Andrey stated, you could just use a function...

Profit = GetProfitString(s);

Upvotes: 7

Akram Shahda
Akram Shahda

Reputation: 14781

Try this:

Profit =  s.SwapProfitAmount.HasValue && s.SwapProfitAmount > 0 ? 
s.SwapProfitAmount.Value.ToString() : 
s.SwapProfitBps && s.SwapProfitBps > 0 ? s.SwapProfitBps.Value.ToString() : "0";

Upvotes: 1

The Lazy Coder
The Lazy Coder

Reputation: 11818

the inline if statement is pretty simple. and if you want to nest it, the simplest way to use it and make it readable is to encase the statements in ()

Result = ( (evaluate Truth) ? (return if True) : (return if False));

// the result of nesting inline if statements.
MyValue = ( [statement] ? ( [another Statement] ? true : false) : ([another statement] ? false : ([another stamement] ? true : false)));

this however can get quite messy. and is the equivalent of actually putting the if statements.

Alternatively if they are nullable. you can do the following.

Profit = ((s.SwapProfitAmount.getValueOrDefault() > s.SwapProfitBps.getValueOrDefault() && s.SwapProfitAmount.HasValue) ? s.SwapProfitAmount.getValueOrDefault() : s.SwapProfitBps.getValueOrDefault();

Upvotes: 4

smartcaveman
smartcaveman

Reputation: 42246

The format for the ternary operator in C# is :

          (condition) 
                      ? (value-if-true) 
                      : (value-if-false)

But, because you are only checking for a null, you can use the coalesce operator, which works like:

           (nullableObject1) ?? (nullableObject2)

which is equivalent to

           (nullableObject1 != null) ? nullableObject1 : (nullableObject2)

You can string these operators together, though you should use parentheses to help make your statements clear with the ternary operator. Either approach works

Upvotes: 2

Yet Another Geek
Yet Another Geek

Reputation: 4289

You could try the following

Profit =  (s.SwapProfitAmount.HasValue && s.SwapProfitAmount.Value > 0) ? s.SwapProfitAmount.Value : (s.SwapProfitBps.HasValue && s.SwapProfitBps.Value > 0) ? s.SwapProfitBps.Value : 0) + "";

Upvotes: 1

Andrey
Andrey

Reputation: 60065

Math.Max(s.SwapProfitAmount ?? 0, s.SwapProfitBps ?? 0)

The problem is that you didn't mention what to do if they are both not null and > 0.

If you want string do:

Math.Max(s.SwapProfitAmount ?? 0, s.SwapProfitBps ?? 0).ToString()

Upvotes: 0

Related Questions