Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制作(大部分)等长集_C#_Linq - Fatal编程技术网

C# 制作(大部分)等长集

C# 制作(大部分)等长集,c#,linq,C#,Linq,使用LINQ(或morelinq),如何将未知长度(但较小)的数组划分为偶数集,并在末尾使用较小(不均匀)的集合,但保持每个列表中的顺序 var数组=newint[]{1,2,3,4}; var集=数组。某物(3); 寻找以下结果: {1,2}、{3}、{4} {1} -> {1},{},{} {1,2} -> {1},{2},{} {1,2,3} -> {1},{2},{3} {1,2,3,4} -> {1,2},{3},{4} {1,2,3,4,5} -> {

使用LINQ(或morelinq),如何将未知长度(但较小)的数组划分为偶数集,并在末尾使用较小(不均匀)的集合,但保持每个列表中的顺序

var数组=newint[]{1,2,3,4};
var集=数组。某物(3);
寻找以下结果:
{1,2}、{3}、{4}

{1} -> {1},{},{}
{1,2} -> {1},{2},{}
{1,2,3} -> {1},{2},{3}
{1,2,3,4} -> {1,2},{3},{4}
{1,2,3,4,5} -> {1,2},{3,4},{5}
{1,2,3,4,5,6} -> {1,2},{3,4},{5,6}
我的原始代码:

const int MaxPerColumn = 6;
var perColumn = (list.Count + columnCount - 1) / columnCount;
for (var col = 1; col <= columnCount; col++)
{
    var items = list.Skip((col - 1) * columnCount).Take(perColumn).Pad(MaxPerColumn, "").ToList();

    // DoSomething
}
const int maxpocolumn=6;
var perColumn=(list.Count+columnCount-1)/columnCount;
对于(var col=1;col我建议此处不使用Linq,而是在实现中使用
IEnumerator
,甚至不使用
IEnumerable

public static IEnumerable<T[]> Something<T>(this IEnumerable<T> source, int count) {
  if (null == source)
    throw new ArgumentNullException("source");
  else if (count <= 0)
    throw new ArgumentOutOfRangeException("count");

  int c = source.Count();
  int size = c / count + (c % count > 0 ? 1 : 0);
  int large = count - count * size + c;    

  using (var en = source.GetEnumerator()) {
    for (int i = 0; i < count; ++i) {
      T[] chunk = new T[i < large ? size : size - 1];

      for (int j = 0; j < chunk.Length; ++j) {
        en.MoveNext();

        chunk[j] = en.Current;
      }

      yield return chunk;
    }
  }
}
这是一条灵巧的路

public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int count)
{
    int c = source.Count();
    int chunksize = c / count + (c % count > 0 ? 1 : 0);

    while (source.Any())
    {
        yield return source.Take(chunksize);
        source = source.Skip(chunksize);
    }
}
公共静态IEnumerable块(此IEnumerable源,int计数)
{
int c=source.Count();
int chunksize=c/count+(c%count>0?1:0);
while(source.Any())
{
收益返回源。获取(chunksize);
source=source.Skip(chunksize);
}
}

基于和

那么为什么{1,2,3.4}是{1,2}、{3}、{4}和{1,2,3,4,5,6}是{1,2}、{3,4}、{5,6}。该模式实际上并不一致?我认为您应该明确说明您的示例用于创建3个组。您尝试了做什么?您的实现遇到了哪些问题?您是否在寻找类似的内容:?为什么人们会问“使用Linq,我如何…?”为什么不只是“我如何…?”为什么必须使用Linq?
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int count)
{
    int c = source.Count();
    int chunksize = c / count + (c % count > 0 ? 1 : 0);

    while (source.Any())
    {
        yield return source.Take(chunksize);
        source = source.Skip(chunksize);
    }
}