Foreach or is within an Anynimous Type C #

1

I am putting together a return of an Api in JSON , and the return I am in AnynimousType I believe that is how it is called, in it I name the variables as they will for Json, I needed to make a foreach to return the parcels, of a form of payment, as in the Example below:

//Pega as formas de pagamento do Banco de dados
        var formas = _ctx.FormasPagamento.Where(e => e.DataExclusao == null && e.EmpresaID == 1).ToList();

        //Monta o retorno em JSON usando AnynimousType
        return Json(formas.Select(e => new
        {
            e.Descricao,
            e.DiaAdicional,
            QtdParcelas = e.Parcelas,
            e.PctTaxaFinanceira,
            PrazoMedio = e.FormaPagamentoParcelas.Select(x => x.Prazo).DefaultIfEmpty(0).Sum() / e.Parcelas,
            PerPgDup = e.Parcelas / 100,

            //Aqui neste trecho seria onde eu precisava montar uma lista com as parcelas desta forma de pagamento..
            //Tentei dessa forma mas o codigo nao reconhece a operação
            ParcelasPgto = new
            {
                foreach(var forma in e.FormaPagamentoParcelas) {
                forma.Prazo,
                }
            } 

        }));

The above code does not work, and would be what I would need to do, doing with Models Json serializes right, I will show an example of the return I needed to use using Models, which I did not want because it has to create Extras classes in project without "Need" ..

MODEL CLASSES:

public class FormaPgto
{
    public FormaPgto()
    {
        ParcelasPgtos = new List<ParcelaPgto>();
    }

    public int Id { get; set; }
    public string Descricao { get; set; }
    public int DiaAdicional { get; set; }
    public int QtdParcelas { get; set; }
    public decimal TaxaFinanceira { get; set; }
    public int PrazoMedio { get; set; }

    public int PerPgDup { get; set; }

    public List<ParcelaPgto> ParcelasPgtos { get; set; }
}

public class ParcelaPgto
{
    public int Prazo { get; set; }
}

METHOD:

 //Pega as formas de pagamento do Banco de dados
        var formas = _ctx.FormasPagamento.Where(e => e.DataExclusao == null && e.EmpresaID == 1).ToList();

        //Aqui é a classe de retorno que eu criei
        var formasRetorno = new List<FormaPgto>();

        //Percorre as formas de pagamento
        foreach (var forma in formas)
        {
            //Mapeia a classe usado como retorno para o Json serializar
            var frm = new FormaPgto();
            frm.Id = forma.ID;
            frm.Descricao = forma.Descricao;
            frm.DiaAdicional = forma.DiaAdicional;
            frm.QtdParcelas = forma.Parcelas;
            frm.TaxaFinanceira = forma.PctTaxaFinanceira;
            frm.PrazoMedio = forma.FormaPagamentoParcelas.Select(x => x.Prazo).DefaultIfEmpty(0).Sum() /
                             forma.Parcelas;
            frm.PerPgDup = forma.Parcelas / 100;

            //Percorre as parcelas da forma de pagamento, como necessitava no outro código
            foreach (var parc in forma.FormaPagamentoParcelas)
            {
                //Instancia a model da parcela
                var pct = new ParcelaPgto();
                //Mepeia
                pct.Prazo = parc.Prazo;
                //Adiciona a uma lista na forma de pagamento
                frm.ParcelasPgtos.Add(pct);
            }
            formasRetorno.Add(frm);
        }

        //Retorna em Json
        return Json(formasRetorno);

The above method with the classes returns me the Json as needed, according to the model below:

In this way it works, but you have to create model classes and the work is bigger, and mapping with AninimousType besides giving the freedom to Define the names of Json variables at the time of Return, discards the creation of additional models in the project, just to use a return

    
asked by anonymous 01.12.2018 / 06:29

1 answer

4

Using Linq you can write something like the example below, to simplify ...

ParcelasPgto = e.FormaPagamentoParcelas.Select(fp => new { Prazo = fp.Prazo}).ToList();
    
01.12.2018 / 14:38