Generate list dynamically with Linq

Asked

Viewed 75 times

0

I have an API that uses Dotnet’s Dynamic to generate billets. 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 I need to fill 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 cannot generate the items in the body with Linq. I tried to mount the body, and then the items through the String.Format(rudeness! ) How can I do this implementation and generate each pulling item from the bank?

EDIT

Follows 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);
  • 1

    Why don’t you work with a friend Viewmodel?

  • Why use dynamic? I see zero reason for this. Put the guy EndPoints and solve the problem (if this is the one, I think it’s another, the question says it has a problem, but it’s not clear on that.).

  • Maniero is that this Endpoint is a third-party SDK to generate billets in the API. I needed to implement a method to generate the body items in Linq, which generate everything in Runtime

  • Vitor, this is a Webforms project, it would really be easier.

  • but the properties of the anonymous object to mount the body of the boleto will not always be name, amount and value? I believe you could rather generate the body in the Linq query

  • Yes, I try, but he throws this exception: Exception Details: Gerencianet.SDK.Gnexception: { "Property": "/items/0", "message": "Invalid type: array (expected Object)." }

  • seems that error is in assigning the items... each item expects an object (name, amount and value) but vc is assigning an array (probably the array of items that vc has assembled) shows the part of the code that vc is assembling the object body and mainly the completion of the property items.

  • I’ll edit the question

Show 3 more comments

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);
  • it changed the error, now it went to string { "Property": "/items/0", "message": "Invalid type: string (expected Object)." }

  • then you’ll have to change the start code, in Linq do in select select new { name=p.Descricao, amount=1, value=p.ItemValor} and pass cobrancas.Toarray() on the property items in the body

  • Jmslasher, he now says it’s string again. I see that I need to convert it to object { "Property": "/items/0", "message": "Invalid type: string (expected Object)." }

  • weird, why cobrancas should now return objects (anonymous with the name, value and amount properties) and not string.

  • I think I’m almost there. I am trying to bring to my problem the first answer to this question answered here: https://stackoverflow.com/questions/16898731/creating-a-json-array-in-c-sharp

  • Jmslasher, thank you, that implementation worked !!!

Show 1 more comment

Browser other questions tagged

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