2010-08-18 145 views
17

什麼是組的最佳方式的陣列到每個n個元素的數組的列表在c#4如何將一個數組分成一組n個元素?

E.g

string[] testArray = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8" }; 

應分成如果我們n = 3的採取。

string[] A1 = {"s1", "s2", "s3"}; 
string[] A2 = {"s4", "s5", "s6"}; 
string[] A3 = {"s7", "s8"}; 

可能是一種使用LINQ的簡單方法?

回答

23

這將產生具有3種元素的字符串數組的數組:如果它實際上是你用,而不是一般的IEnumerables,特別是如果該陣列是非常大的工作陣列

int i = 0; 
var query = from s in testArray 
      let num = i++ 
      group s by num/3 into g 
      select g.ToArray(); 
var results = query.ToArray(); 
+1

+1,這種方法唯一的缺點是它被熱切地評估。在返回單個元素之前,必須處理整個查詢。 – JaredPar 2010-08-18 17:26:24

+0

@JaredPar:點好了;然而,取決於收藏的大小或處理的性質,懶惰的評估可能被高估。即便如此,+1爲您的解決方案提供了一個有效的懶惰方法。 – kbrimington 2010-08-18 17:30:08

9

我不認爲有一個偉大的內置方法,但你可以寫一個如下。

public static IEnumerable<IEnumerable<T>> GroupInto<T>(
    this IEnumerable<T> source, 
    int count) { 

    using (var e = source.GetEnumerator()) { 
    while (e.MoveNext()) { 
     yield return GroupIntoHelper(e, count); 
    } 
    }  
} 

private static IEnumerable<T> GroupIntoHelper<T>(
    IEnumerator<T> e, 
    int count) { 

    do { 
    yield return e.Current; 
    count--; 
    } while (count > 0 && e.MoveNext()); 
} 
+0

完美答案。但應該提供一個如何解決問題中的問題的例子。 – 2014-12-30 13:23:27

2

,那麼這種方法是一種非常快速和有效的記憶方式。如果你真的只想要一個LINQ語句,那麼不要介意。

private static T[][] SliceArray<T>(T[] source, int maxResultElements) 
    { 
     int numberOfArrays = source.Length/maxResultElements; 
     if (maxResultElements * numberOfArrays < source.Length) 
      numberOfArrays++; 
     T[][] target = new T[numberOfArrays][]; 
     for (int index = 0; index < numberOfArrays; index++) 
     { 
      int elementsInThisArray = Math.Min(maxResultElements, source.Length - index * maxResultElements); 
      target[index] = new T[elementsInThisArray]; 
      Array.Copy(source, index * maxResultElements, target[index], 0, elementsInThisArray); 
     } 
     return target; 
    } 
5
int size = 3; 
var results = testArray.Select((x, i) => new { Key = i/size, Value = x }) 
         .GroupBy(x => x.Key, x => x.Value, (k, g) => g.ToArray()) 
         .ToArray(); 

如果你不介意的結果被分類爲IEnumerable<IEnumerable<T>>而非T[][]那麼您完全可以忽略的ToArray電話:

int size = 3; 
var results = testArray.Select((x, i) => new { Key = i/size, Value = x }) 
         .GroupBy(x => x.Key, x => x.Value); 
1

您可以使用此分機

public static class Extension 
{ 
    private static IEnumerable<TList> Split<TList, T>(this TList value, int countOfEachPart) where TList : IEnumerable<T> 
    { 
     int cnt = value.Count()/countOfEachPart; 
     List<IEnumerable<T>> result = new List<IEnumerable<T>>(); 
     for (int i = 0; i <= cnt; i++) 
     { 
      IEnumerable<T> newPart = value.Skip(i * countOfEachPart).Take(countOfEachPart).ToArray(); 
      if (newPart.Any()) 
       result.Add(newPart); 
      else 
       break; 
     } 

     return result.Cast<TList>(); 
    } 

    public static IEnumerable<IDictionary<TKey, TValue>> Split<TKey, TValue>(this IDictionary<TKey, TValue> value, int countOfEachPart) 
    { 
     IEnumerable<Dictionary<TKey, TValue>> result = value.ToArray() 
                  .Split(countOfEachPart) 
                  .Select(p => p.ToDictionary(k => k.Key, v => v.Value)); 
     return result; 
    } 

    public static IEnumerable<IList<T>> Split<T>(this IList<T> value, int countOfEachPart) 
    { 
     return value.Split<IList<T>, T>(countOfEachPart); 
    } 

    public static IEnumerable<T[]> Split<T>(this T[] value, int countOfEachPart) 
    { 
     return value.Split<T[], T>(countOfEachPart); 
    } 

    public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> value, int countOfEachPart) 
    { 
     return value.Split<IEnumerable<T>, T>(countOfEachPart); 
    } 
} 
相關問題