Use lambda expressions to sophisticate the parameters of a for in c#

Asked

Viewed 66 times

2

good afternoon!

I would like to ask a question, I am developing a collection code and at a certain time it is necessary a to iterate the values of the list and then save the information in an item... So far so good, but the problem arises because the frame in which I am collecting does not have a certain amount of lines for collection, and can be from 1 line to 50(example).

At this moment my logic is broken, because I can not create conditions following a minimal logic, I would like to know what is possible to leave the is auto interpretative, for when it goes through the values, the next lines of the array that it reads, automatically recognizes the value and value in the specific var.

I ended up not posting the code because I don’t know if it would help much, but follow the excerpt of the for and the link of the page I’m capturing:

foreach (var acumulador in item)
{
    var texto = acumulador.SelectNodes("//*[contains(text(),'Código')]/../../descendant-or-self::tr|//*[@class='RelLinhaBranca'][1]//*[contains(text(),'Código')]/../../following-sibling::tr");

    for (int j = 0; j < texto.Count; j++)
    {
        if (texto == null || texto.ToString() == "")
        {
            continue;
        }
        else
        {

            itens.favorecido = new Favorecido() { nome = texto[j + 1].InnerText.CopyUntil("- V").Trim() };
            itens.marca = texto[j + 2].InnerText.CopyAfter("Marca").Trim();
            itens.valor_unitario = decimal.Parse(texto[j + 3].InnerText.CopyBetween("R$", "Valor").Replace(",", "").Trim(), new CultureInfo("pt-BR"));
            itens.valor_total = decimal.Parse(texto[j + 3].InnerText.CopyAfter("Total").Replace("R$", "").Replace(",", "").Trim(), new CultureInfo("pt-BR"));
            itens.num_item = texto[j].InnerText.CopyBetween("Código", "Categoria").Trim();
            itens.tipo_item = texto[j].InnerText.CopyBetween("Categoria", "Subcategoria").Trim();
            itens.descricao = texto[j].InnerText.CopyBetween("Descrição", "Observação").Trim();
            itens.quantidade = decimal.Parse(texto[j].InnerText.CopyBetween("Quantidade", "Tipo").Split()[1].Trim(), new CultureInfo("pt-BR"));
            itens.unidade = texto[j].InnerText.CopyAfter("Cotacao").Split()[2].Trim();
        }
        documento.itens.Add(itens);
        var ex = "";
    }
}

insert link description here

1 answer

3


First, a hint, you are testing text for null but before it is giving a text. Count no up, if it’s null he’ll break there already, not later. But as for your question, I believe this could be solved with lambda using the commands where and select, example of how it would look:

var result = text.Where(x=>!string.IsNullOrEmpty(x)).Select(x=> new Item()
            {

            favorecido = new Favorecido() { nome = x.InnerText.CopyUntil("- 
               V").Trim() };
              //etc

            }).ToList();

This would result in an item list (List), so instead of add you would probably need to use Addrange

documento.itens.AddRange(result);

Browser other questions tagged

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