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

Good afternoon!

I would like to take a question, I am developing a collection code and at a certain point if it is necessary to iterate the values of the list and then save the information in an item... So far everything is fine, but the problem arises because the frame in which I am collecting does not have a certain amount of rows to collect, being able to be from 1 row to 50(example).

Right now my logic breaks, because I can't create conditions following a minimal logic, I would like to know what the possibility of leaving the for self-interpretive, for when it passes through the values, the next rows of the array that it reads, automatically it recognizes the value and allocates in the specific var.

I ended up not posting the code because I do not know if it would help much, but follow the excerpt of the for and the link of the page I am 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

Author: Leandro Angelo, 2018-11-13

1 answers

First, a hint, you are testing text for null but before it is giving a text.Count no is up, if it is null it will break there already, not later. But as for your question, I believe this could be solved with lambda using the where and select commands, 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 a list of items, so instead of add you would probably need to use AddRange

documento.itens.AddRange(result);
 3
Author: Lucas Miranda, 2018-11-13 14:53:17