Is there a way to move from listT to ListListT with Linq?

-1

I have made a code that does the following:

public List<List<object>> SegmentarLista(List<object> origen, tamaño){
    resultado= new List<List<object>>();
    for (int i=0; i<origen.Length;i+tamaño){
         resultado.Add(origen.GetRange(i,tamaño))
    }

}

Would there be any way to do this with Linq without the for?

edit: The question is not in search of efficiency, nor readability. It is pure curiosity about how to achieve the same in a more functional way, with immutable variables.

    
asked by Adrian Godoy 22.08.2018 в 19:18
source

1 answer

0

An example of code in which a list is divided into rows of 2

 var vector = new List<int>() { 1, 2, 3, 4 };
 var result= vector.Select((num, indexer) => new { Fila = indexer/2, Value = num })
            .GroupBy(item=>item.Fila)
            .Select(grp=>grp.Select(i=>i.Value).ToList())
            .ToList();

Maybe there is some more elegant way, but with group by we can make this transformation easily and on a personal level it seems to me a more understandable code.

    
answered by 23.08.2018 / 15:42
source