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);
Why don’t you work with a friend Viewmodel?
– Victor Laio
Why use
dynamic
? I see zero reason for this. Put the guyEndPoints
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
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
– Claudinei Ferreira
Vitor, this is a Webforms project, it would really be easier.
– Claudinei Ferreira
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
– JMSlasher
Yes, I try, but he throws this exception: Exception Details: Gerencianet.SDK.Gnexception: { "Property": "/items/0", "message": "Invalid type: array (expected Object)." }
– Claudinei Ferreira
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 propertyitems
.– JMSlasher
I’ll edit the question
– Claudinei Ferreira