开发者

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开发者_StackOverflow中文版 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.


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


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();


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


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) + "";


Try this:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜