2011-03-07 11 views

risposta

15
var groups = allPendingPersons.Select((p, index) => new {p,index}) 
           .GroupBy(a =>a.index/10); 

se si desidera elaborare IGrouping<,>. Se siete alla ricerca di Lista> indietro si potrebbe provare

var listOfLists = allPendingPersons.Select((p, index) => new {p, index}) 
    .GroupBy(a => a.index/10) 
    .Select((grp => grp.Select(g => g.p).ToList())) 
    .ToList(); 
+0

non è il più efficiente, GroupBy bufferizzerà tutti gli elementi e costruirà la ricerca. Quindi, memoria + CPU in testa + non una soluzione di vaporizzazione –

46

È possibile scrivere il proprio metodo di estensione:

public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> sequence, int size) { 
    List<T> partition = new List<T>(size); 
    foreach(var item in sequence) { 
     partition.Add(item); 
     if (partition.Count == size) { 
      yield return partition; 
      partition = new List<T>(size); 
     } 
    } 
    if (partition.Count > 0) 
     yield return partition; 
} 

ho explored this in more depth nel mio blog.

+1

leggermente migliore da fare: 'partizione = new List (dimensione);' – nawfal

+0

@nawfal: Hai ragione; fisso. – SLaks

+0

questo è così buono! L'ho usato direttamente sulla mia lista di articoli che necessitavano di azione. foreach (var b in senders.Partition (threshold)) {handleBatch (b); } semplice ed elegante, grazie! –

2

Non è la tecnica più efficiente, ma questo produrrà una sequenza IEnumerable<IEnumerable<Person>>, con ogni sequenza interna contenente dieci elementi:

var query = allPendingPersons.Select((x, i) => new { Value = x, Group = i/10 }) 
          .GroupBy(x => x.Group, 
             (k, g) => g.Select(x => x.Value)); 

e se il risultato ha veramente bisogno di essere un elenco-di-liste, piuttosto che una semplice sequenza quindi è possibile creare un List<List<Person>> invece con l'aggiunta di un paio di chiamate ToList:

var query = allPendingPersons.Select((x, i) => new { Value = x, Group = i/10 }) 
          .GroupBy(x => x.Group, 
             (k, g) => g.Select(x => x.Value).ToList()) 
          .ToList(); 
+1

È possibile semplificare la prima versione un po 'scrivendo 'x => x.Value' invece di' (k, g) => g.Seleziona (x => x. valore) '. –

0

provare un blocco iteratore:

public static IEnumerable<List<Person>> AsGroups(this List<Person> persons) 
{ 
    var buf = new List<Person>(10); 
    for (int i = 0; i<persons.Count i++;) 
    { 
     buf.Add(persons[i]); 
     if (i%10 == 0 && buf.Count > 0) 
     { 
      yield return buf; 
      buf = new List<Person>(10); 
     } 
    } 
    yield return buf; 
} 
6

Il Reactive Extensions for .NET (Rx) ha un metodo di estensione che fa esattamente ciò che si vuole:

var buffered = allPendingPersons.BufferWithCount(10); 

Se si vuole farlo utilizzando LINQ si potrebbe fare questo:

var buffered = 
    allPendingPersons 
     .Select((p, i) => new { Group = i/10, Person = p }) 
     .GroupBy(x => x.Group, x => x.Person) 
     .Select(g => g.ToArray()); 
4
+0

+1 per 'Batch' di MoreLink, [vedi fonte qui] (https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/Batch.cs) – lagerone

0

C'è un modo elegante in LINQ

La elegant way non è molto performante. Ecco un modo più performante ...

public static List<List<T>> Chunk<T>(
     this List<T> theList, 
     int chunkSize 
    ) 
    { 
     if (!theList.Any()) 
     { 
      return new List<List<T>>(); 
     } 

     List<List<T>> result = new List<List<T>>(); 
     List<T> currentList = new List<T>(); 
     result.Add(currentList); 

     int i = 0; 
     foreach(T item in theList) 
     { 
      if (i >= chunkSize) 
      { 
       i = 0; 
       currentList = new List<T>(); 
       result.Add(currentList); 
      } 
      i += 1; 
      currentList.Add(item); 
     } 
     return result; 
    } 
+1

Puoi essere più performante di ciò scrivendo un iteratore. Vedi la mia risposta. – SLaks

Problemi correlati