Generate list dynamically with Linq

0

I have an API that uses DotNet dynamic to generate the tickets. The documentation is as follows:

dynamic endpoints = new Endpoints("client_id", "client_secret", true);

var body = new
{
items = new[] {
    new {
        name = "Primeiro item da descrição",
        value = 1000,
        amount = 1
    },
    new {
        name = "Segundo item da descrição",
        value = 1000,
        amount = 1
        },
    },
};

var response = endpoints.CreateCharge(null, body);

This is my query that I need to fill out the items in the body:

var itensCobranca = from p in db.DetalhesCobrancas
                where p.CobrancaId == 2
                select p;

    foreach(var item in itensCobranca)
    {
        name = item.Descricao;
        amount = item.Qtde;
        value = item.valor;
    }

The problem is that as it comes to dynamic items I can not generate the items in the body with Linq. I tried to mount the body, and then the items through String.Format (rudeness!) How can I make this implementation and generate each item by pulling the bank?

EDIT

Follow the block

gerencianetDataContext db = new gerencianetDataContext();
        var cobrancas = (from p in db.DetalhesCobrancas
                         where p.CobrancaId == 2
                         select p).ToList();
        foreach (var item in cobrancas)
        {
            string itens = String.Format("new {{name = \"{0}\",value = {1},amount = 1}},", item.Descricao, item.IntValor);

            Literal.Text += itens;
        }

        string items = Literal.Text;//define string puro dos itens
        //agora o corpo

        //tentando transformar em objeto
        List<object> Obj1AsObjects = items.Cast<object>().ToList();

        //gerando o boleto
        dynamic endpoints = new Endpoints("Client_Id", "Client_Secret", true);
        var body = new
        {
            items = new[]{
                Obj1AsObjects
            }
        };

        var response = endpoints.CreateCharge(null, body);
    
asked by anonymous 30.11.2018 / 12:50

1 answer

1

Try this change (complete):

gerencianetDataContext db = new gerencianetDataContext();
var cobrancas = (from p in db.DetalhesCobrancas
                 where p.CobrancaId == 2
                 select new {
                     name = p.Descricao,
                     value = p.ItemValor,
                     amount = 1
                 });

//gerando o boleto
dynamic endpoints = new Endpoints("Client_Id", "Client_Secret", true);
var body = new
{
    items = cobrancas.ToArray()
};

var response = endpoints.CreateCharge(null, body);
    
30.11.2018 / 14:02