Create Data Groups with Linq

0

I have a question with linq. here I consult a group of users, in this case 250 arrive.

var usuarios = _db.usuariosmv10.Where(x => x.campana.Equals(id)).ToList();

So, in the users variable I have a list of 250 users, now that's what I need.

I want to be able to group these 250 users into groups of 50, so I have a list of 5 left in a variable where those 5 contain 50 users, how is that possible?

I appreciate the advice.

    
asked by Cristian 29.09.2017 в 21:43
source

1 answer

1

Try the following:

var usuariosAgrupados = usuarios.Select((x, i) => new { Index = i, Value = x })
                                .GroupBy(x => x.Index / 50)
                                .Select(x => x.Select(m => m.Value).ToList())
                                .ToList();

Brief explanation: First a new list is created, in which the index of each element is added to the original list. Then a grouping by index is made (every 50 elements). Finally the elements of this new grouped list are obtained.

    
answered by 30.09.2017 / 01:01
source