How to walk a C#class?

Asked

Viewed 198 times

-1

I have the DeserializeObject that fills in an order class and items, on the items I need to go through this class to record the data, the way I’m doing is zeroing the records, how I could read the data?.

public class Item
{
    public string idItemPedido { get; set; }
    public int item { get; set; }
    public string plano { get; set; }
    public Produto produto { get; set; }
    public string descricaoItem { get; set; }
    public decimal valorUnitario { get; set; }
    public string qtde { get; set; }
    public decimal valorTotal { get; set; }
    public string pontos { get; set; }
}

   string teste = "115873";
   GravaItensPedido(teste);


   public void GravaItensPedido(string CodigoCabecahoPedido)
        {
            var strQuery = "";
            IEnumerable<Item> enumeracao = new List<Item>();

            foreach (var item in enumeracao)
            {
                strQuery = "";
            }
        }

2 answers

0

You want to generate an update/Insert query for class properties?

If so, I recommend using Dapper to help you. It’s a C# library created by the Stackoverflow team.

It’ll help you a lot: Dapper

Look how cool:

var count = connection.Execute(@"
  set nocount on 
  create table #t(i int) 
  set nocount off 
  insert #t 
  select @a a union all select @b 
  set nocount on 
  drop table #t", new {a=1, b=2 });
Assert.Equal(2, count);

You take an ADO.NET connection, you’ll have an extension method called Execute. Then you call him with 2 parameters.

  • in 1 parameter you pass sql
  • in the 2nd parameter you pass an object, with the data for it to mount the query for you.

How about?

  • I appreciate the help no more and that, as I said in my question, the class is already filled with the data that came from Json, so I said that I had made the Deserializeobject , what I need and go through the records that are in this class.

  • You access its properties without having to directly reference one-to-one? If so, you can use Reflection. Know?

  • Gabriel, see how clear it is :https://answall.com/questions/262920/ienumerable-%C3%A9-o-melhor-para-consultar-dados-de-cole%C3%A7%C3%B5es-na-Mem%C3%B3ria

0

There’s something missing from your question, so

case 1 : Voce informs the parameter(codeCabecahoPedido) and receives a list using some method

public void GravaItensPedido(string codigoCabecahoPedido)
{

      List<Item> minhaLista= getlistblabla(x=>x.CodigoCabecahoPedido == codigoCabecahoPedido);

      foreach (var itens in minhaLista)
      {
         //Faz alguma coisa
      }
}

case 2: Pass the list that will be changed or something like

public void GravaItensPedido(string codigoCabecahoPedido, List<Item> itens)
{      
  var strQuery = "";

  foreach (var itens in itens)
  {
      if(codigoCabecahoPedido == "test")
      {
        //faca alguma coisa
      }
  }
}
  • Hudson, I adjusted my question, but the solution did not work, if it is possible to help I thank

  • can you rephrase your question? I still can’t understand what you really want

  • Hudson, see if you can clarify https://answall.com/questions/262920/ienumerable-%C3%A9-o-melhor-para-consultar-dados-de-cole%C3%A7%C3%B5es-na-Mem%C3%B3ria

  • that (string Codigocabecahopedido) is your json?

Browser other questions tagged

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