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

2

Good afternoon!

I would like to ask a question, I am developing a collection code and at a certain point if it takes a for iterate the values in the list and then save the information in an item ... Until then, however, the problem arises because the frame in which I am collecting does not have a certain amount of lines for collection, it 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 the possibility of leaving it to be auto-interpretative, when it goes through the values, the next lines of the array that it reads, automatically recognizes the value and allocates the specific var.

I just did not post the code because I do not know if it would help a lot, but it follows the excerpt from 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 the link description here

    
asked by anonymous 13.11.2018 / 14:43

1 answer

3

First, a tip, you are testing text to null but before it is giving text.Count in the above, if it is null it will break there already, not after. But as for your question, I believe this could be solved with lambda using the commands where and select , example of what it would look like:

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 (List), so instead of add you probably would need to use AddRange

documento.itens.AddRange(result);
    
13.11.2018 / 15:53