No. The ternary operator only works with two expressions.
The most you can do is something similar to the code below, nesting the ternaries.
Keep in mind that this makes code extremely difficult to read.
var resultado = total < 5 ?
((total > 0) ? "Menor que cinco, mas maior que zero"
: "Menor que cinco, zero ou menos")
: "Maior ou igual a cinco";
See working on . NET Fiddle
Editing
As I know that you are an admirer of expressions, Lambdas, functions and etc. And I also remember a question that you deleted where you asked something like avoid a lot of if’s using expressions, I decided to write a code with this adapted to the current problem.
Keep in mind that this is not a concrete solution given the circumstances, you can rather take advantage of this code, but I believe not for the current problem.
Explanation: The code is basically based on a list of key-value pairs, where each element of this list has as its key an expression that takes as input (parameter) a decimal number and returns a boolean
(Func<decimal, bool>
). The values of these pairs are one string descriptive with the result, obviously this can be switched to anything, but I decided to leave so to get more illustrative.
The method ValidarTotal
iterates over all these items, calls the function (which is the key of the pair) passing the parameter valor
as a function input and, if the result is true
, returns the value of the pair. Obviously it is possible to change so that it is grouped, I did this in the method ValidarTotalAgrupado
. His idea is the same as the first, with the difference that will return a string with all values where the function returned true
and that I used LINQ.
Code:
KeyValuePair<Func<decimal, bool>, string>[] pares = new[]
{
new KeyValuePair<Func<decimal, bool>, string>(valor => valor > 5, "Maior que 5"),
new KeyValuePair<Func<decimal, bool>, string>(valor => valor > 0 && valor < 5, "Maior que 0 e menor que 5"),
new KeyValuePair<Func<decimal, bool>, string>(valor => valor > 0, "Número positivo")
};
static string ValidarTotalAgrupado(decimal valor, params KeyValuePair<Func<decimal, bool>, string>[] pares)
{
var ret = pares.Where(p => p.Key(valor))
.Select(p => p.Value)
.Aggregate((a, b) => $"{a}, {b}");
return ret;
}
static string ValidarTotal(decimal valor, params KeyValuePair<Func<decimal, bool>, string>[] pares)
{
foreach(var item in pares)
{
if(item.Key(valor)) {
return item.Value;
}
}
return null;
}
void Main()
{
var testes = new[]
{
new { Valor = 6m },
new { Valor = 2m },
new { Valor = 0.5m }
};
foreach(var teste in testes)
{
var res = ValidarTotal(teste.Valor, pares);
var resAgrupado = ValidarTotalAgrupado(teste.Valor, pares);
Console.WriteLine($"Valor: {teste.Valor:n2}\nResultado: {res}\nAgrupado: {resAgrupado}\n");
}
}
See working on . NET Fiddle.
Actually I need to paint a Boxview depending on the result. I tried this but I can’t put boxview to paint.
– pnet
Any solution proposed will have to follow one of the 3 possible paths. And that I know has no way to do in a simplified way. Most likely it encapsulates itself in a method.
– Marco Souza