Operator ?: does not work

Asked

Viewed 101 times

1

I’m doing the union, in one of select i need to make a condition to appear in the correct way. To not keep appearing blank values.

I tried to do it this way:

      Documento = (c.FaturaContasReceberP.NotaFiscal != null ? "Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal : null +
         c.FaturaContasReceberP.NotaFiscalProdutos != null ? " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos : null).Trim()

But it does not work as expected, here is the complete part of the code:

 var FR = db.FaturaContasReceber.Include(c => c.FaturaContasReceberP).Where(c => c.Quitado == true && c.Caixa == false && c.BancoId == id).Select(c => new BancoList
        {
            Id = c.Id,
            Data = c.DataPagamento,
            Valor = c.Total,
            Tipo = "C",
            Documento = (c.FaturaContasReceberP.NotaFiscal != null ? "Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal : null +
         c.FaturaContasReceberP.NotaFiscalProdutos != null ? " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos : null).Trim()
        }).ToList();

How to solve, I need it to appear the field if it has filled in, it may occur of only one of the two being filled in, or of the two.

  • To my knowledge, C# does not accept a question mark within the if, may have some other special character to replace it

  • Sorry, I edited the question, I’m not using the if

  • tries to put the operation in parentheses, like ("Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal).. c# may be getting lost with the signs

  • @Since it only appears the values of Nfse, even if Nfe is not null it does not appear, and when only Nfe has value, the Documento empty.

3 answers

3

Another solution:

Documento = (c.FaturaContasReceberP?.NotaFiscal?.ToString().Insert(0, "Fat. NFSe: ") + c.FaturaContasReceberP?.NotaFiscalProdutos?.ToString().Insert(0, " NFe: ")).Trim();

Should the type of NotaFiscal and NotaFiscalProdutos be it string, can exclude .ToString()

?. Conditional null and void operators

Tests the value of the left operand for null before executing a member access (?.) or an index operation (?[]); returns null if the left operand is evaluated as null.

2

Tries to place parentheses to isolate the internal ternary operator:

Documento = (c.FaturaContasReceberP.NotaFiscal != null ? 
                "Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal : 
                (null + c.FaturaContasReceberP.NotaFiscalProdutos != null ? 
                    " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos : 
                    null
                )
            ).Trim();

[Edited] Tries to validate c.Invoicesreceberp is null:

Documento = (c.FaturaContasReceberP != null ? (c.FaturaContasReceberP.NotaFiscal != null ? 
                "Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal.Trim() : 
                (c.FaturaContasReceberP.NotaFiscalProdutos != null ? 
                    "NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos.Trim() : 
                    ""
                )
            ) : "");
  • Also does not work, only shows the first one that is Nfse even if Nfe not being null does not appear, and if only Nfe has the value, it does not appear anything.

  • Set validating if c.Invoicesreceberp is null. If it still doesn’t work, set the error that is appearing in the debug.

2


I believe the problem is in that part Fiscal : null + c.FaturaContasReceberP

Try to do it this way by separating logic in parentheses.

Documento = (
    (c.FaturaContasReceberP.NotaFiscal != null ? "Fat. NFSe: " + c.FaturaContasReceberP.NotaFiscal : "") +
    (c.FaturaContasReceberP.NotaFiscalProdutos != null ? " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos : "")
).Trim()
  • Returns the following error: Conditional expression type cannot be determined because there is no implicit conversion between "string" and "char"

  • 1

    @marianac_costa change the quotes by quotation marks

  • Gave straight, rs @Vik had not even attacked me to this detail. Thank you.

  • I edited the answer with the correction, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.