Mariana
Mariana

Reputation: 170

How to select entity properties based on a condition?

I have this select:

 var CP = db.ContasPagar.Include(c => c.ContasPagarP).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 = "D",
            Documento = "Fat. Contas Diversas " + c.ContasPagarP.Codigo,

        }).ToList();
        var FP = db.FaturaContasPagar.Include(c => c.FaturaContasPagarP).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 = "D",
            Documento = "Fat. Nota Fiscal: " + c.FaturaContasPagarP.NotaFiscal,
        }).ToList();
        var CR = db.ContasReceber.Include(c => c.ContasReceberP).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 = "Fat. Contas Diversas " + c.ContasReceberP.Codigo,
        }).ToList();
        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,
        }).ToList();
        var BC = db.BancoMovimento.Where(c => c.Credito == true && c.ContaId == id).Select(c => new BancoList
        {
            Id = c.Id,
            Data = c.DataMovimento,
            Valor = c.Valor,
            Tipo = "C",
            Documento = c.Historico
        }).ToList();
        var BD = db.BancoMovimento.Where(c => c.Credito == false && c.ContaId == id).Select(c => new BancoList
        {
            Id = c.Id,
            Data = c.DataMovimento,
            Valor = c.Valor,
            Tipo = "D",
            Documento = c.Historico
        }).ToList();

        var union = CP.Union(FP).Union(FR).Union(CR).Union(BC).Union(BD).OrderBy(c => c.Data).ToList();

Only a problem occurs, not always the Nfse is filled, and not always the NotaFiscalProdutos is filled, I need it to check for these values, to add into the Documento.

If only the NumeroNfse field has value, would look like this: Fat. NFSe: " + c.FaturaContasReceberP.NFSe.NumeroNfse and if only NotaFiscalProdutos retorar " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos, and if both have return the two.

EDIT

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

I changed to this form, but also does not work as expected.

Upvotes: 0

Views: 63

Answers (2)

41686d6564
41686d6564

Reputation: 19641

This would be a long statement if you put it in the Select and would affect the readability of your code significantly. Here's one way to get around this...

In your entity that belongs to FaturaContasReceber, create a read-only property that looks like this:

public string Documento
{
    get
    {
        string nfse = FaturaContasReceberP?.NFSe?.NumeroNfse;
        string nfe = FaturaContasReceberP?.NotaFiscalProdutos;
        string documento = (nfse ? $"Fat. NFSe: {nfse}" : "") +
                           (nfe ? $" NFe: {nfe}" : "");
        return documento.Trim();
    }
}

Then you can easily use it like this:

var FR = db.FaturaContasReceber
                .Include(c => c.FaturaContasReceberP)
                .Where(c => c.Quitado && !c.Caixa && c.BancoId == id)
                .ToList()
                .Select(c => new BancoList
                {
                    Id = c.Id,
                    Data = c.DataPagamento,
                    Valor = c.Total,
                    Tipo = "C",
                    Documento = c.Documento
                }).ToList();

Edit:

If you insist on doing it in the Select, you can do it like this:

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

But I would advise you to use the first solution.

Upvotes: 2

TSungur
TSungur

Reputation: 406

You can use the ?:operator

    Tipo = "C",
Documento = c.FaturaContasReceberP.NFSe != null ? "Fat. NFSe: " + c.FaturaContasReceberP.NFSe.NumeroNfse:null + c.FaturaContasReceberP.NotaFiscalProdutos!=null? " NFe: " + c.FaturaContasReceberP.NotaFiscalProdutos:null,

Upvotes: 1

Related Questions