如何创建,创建最多100个对象组一个LINQ表达式 [英] How do I create a Linq expression that creates groups of max 100 objects

查看:183
本文介绍了如何创建,创建最多100个对象组一个LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ表达式从Azure的表存储组客户的分区。

I have a LINQ expression that groups customers from an Azure Table Storage by partition.

由于Azure的只支持100的实体在同一时间,最大批处理操作(和实体在批量多有相同PartitionKey),我需要每个小组包含最多100元。

Because Azure only supports batch operations with max 100 entities at a time (and entities in a batch much have the same PartitionKey), I need each group to contain a maximum 100 entities.

//How to complete this LINQ expression
var groups = customers.GroupBy(c => c.PartitionKey)....;

//Do some Azure table storage magic in parallel
Parallel.ForEach(groups , customersInGroup => {...});



我如何完成我的LINQ表达式,所以每个组包含100最大客户?这是...如果客户收集如。拥有142客户拥有相同的PartitionKey,我想创建两个组...一组,100个客户,一个有42客户。

How do I complete my LINQ expression, so each group contains max 100 customers? That is... if the customers collection eg. has 142 customers with the same PartitionKey, i want to create two groups... one groups with 100 customers and one with 42 customers.

推荐答案

有没有什么是在正常的LINQ直接做到这一点,但 MoreLINQ 拥有的 批量 方法您可能会发现有用的:

There's nothing within "normal" LINQ to do this directly, but MoreLINQ has a Batch method which you may find useful:

public static IEnumerable<TResult> Batch<TSource, TResult>
    (this IEnumerable<TSource> source, int size,
     Func<IEnumerable<TSource>, TResult> resultSelector)

public static IEnumerable<IEnumerable<TSource>> Batch<TSource>
    (this IEnumerable<TSource> source, int size)

请注意,在你的情况倒是可能想是这样的:

Note that in your case you'd probably want something like:

var groups = customers.GroupBy(c => c.PartitionKey).Batch(100, p => p.ToList());



使返回的结果立即兑现。

so that the returned results are materialized immediately.

当然,这是假设你使用LINQ到对象 - 如果你想通过其他LINQ提供程序分区,我不知道你会如何去了解它。

Of course, this is assuming you're using LINQ to Objects - if you're trying to partition via another LINQ provider, I'm not sure how you'd go about it.

这篇关于如何创建,创建最多100个对象组一个LINQ表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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