创建LINQ批 [英] Create batches in linq

查看:102
本文介绍了创建LINQ批的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能提出一个方法在LINQ创造了一定的大小批量?

Can someone suggest a way to create batches of a certain size in linq?

理想我希望能够在某些配置量的块执行操作

Ideally I want to be able to perform operations in chunks of some configurable amount.

推荐答案

您不需要编写任何code。使用 MoreLINQ 批次方法,分批源序列插入水桶大小(MoreLINQ可作为您可以安装的NuGet包):

You don't need to write any code. Use MoreLINQ Batch method, which batches the source sequence into sized buckets (MoreLINQ is available as a NuGet package you can install):

int size = 10;
var batches = sequence.Batch(size);

哪个被实现为:

public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
                  this IEnumerable<TSource> source, int size)
{
    TSource[] bucket = null;
    var count = 0;

    foreach (var item in source)
    {
        if (bucket == null)
            bucket = new TSource[size];

        bucket[count++] = item;
        if (count != size)
            continue;

        yield return bucket;

        bucket = null;
        count = 0;
    }

    if (bucket != null && count > 0)
        yield return bucket.Take(count);
}

这篇关于创建LINQ批的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆