将列表分组为每个组的X个项目组 [英] Grouping lists into groups of X items per group

查看:103
本文介绍了将列表分组为每个组的X个项目组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,那就是要知道制作一个方法将项目列表分组(例如)不超过3个项目的最佳方法。我已经创建了下面的方法,但是在我返回之前没有在组上执行 ToList ,如果列表多次枚举,那么我有问题。



第一次枚举是正确的,但是任何额外的枚举都会被抛出,因为两个变量(i和groupKey)似乎在迭代之间被记住。



所以问题是:


  • 有没有更好的方法来做我想要的实现?

  • 仅仅是在离开这个方法之前列出所得到的组
    真的是一个坏主意?

      public static IEnumerable< IGrouping< int,TSource>>的GroupBy< TSource> 
    (这个IEnumerable< TSource>源,int itemsPerGroup)
    {
    const int initial = 1;
    int i = initial;
    int groupKey = 0;

    var groups = source.GroupBy(x =>
    {
    if(i == initial)
    {
    groupKey = 0;


    if(i> initial)
    {
    //如果我们已经计算出每组物品的数量,则增加组键值
    if(itemsPerGroup ==初始|| i%itemsPerGroup == 1)
    {
    groupKey ++;
    }
    }

    i ++;

    返回groupKey;
    });

    返回组;



解决方案<

  public static IEnumerable< IGrouping< int,TSource> ;>的GroupBy< TSource> 
(这个IEnumerable< TSource>源,int itemsPerGroup)
{
返回source.Zip(Enumerable.Range(0,source.Count()),
(s,r )=> new {Group = r / itemsPerGroup,Item = s})
.GroupBy(i => i.Group,g => g.Item)
.ToList()
}

现场演示


I'm having a problem knowing the best way to make a method to group a list of items into groups of (for example) no more than 3 items. I've created the method below, but without doing a ToList on the group before I return it, I have a problem with it if the list is enumerated multiple times.

The first time it's enumerated is correct, but any additional enumeration is thrown off because the two variables (i and groupKey) appear to be remembered between the iterations.

So the questions are:

  • Is there a better way to do what I'm trying to achieve?
  • Is simply ToListing the resulting group before it leaves this method really such a bad idea?

    public static IEnumerable<IGrouping<int, TSource>> GroupBy<TSource>
                  (this IEnumerable<TSource> source, int itemsPerGroup)
    {
        const int initial = 1;
        int i = initial;
        int groupKey = 0;
    
        var groups = source.GroupBy(x =>
        {
            if (i == initial)
            {
                groupKey = 0;
            }
    
            if (i > initial)
            {
                //Increase the group key if we've counted past the items per group
                if (itemsPerGroup == initial || i % itemsPerGroup == 1)
                {
                    groupKey++;
                }
            }
    
            i++;
    
            return groupKey;
        });
    
        return groups;
    }
    

解决方案

Here's one way to do this using LINQ...

public static IEnumerable<IGrouping<int, TSource>> GroupBy<TSource>
    (this IEnumerable<TSource> source, int itemsPerGroup)
{
    return source.Zip(Enumerable.Range(0, source.Count()),
                      (s, r) => new { Group = r / itemsPerGroup, Item = s })
                 .GroupBy(i => i.Group, g => g.Item)
                 .ToList();
}

Live Demo

这篇关于将列表分组为每个组的X个项目组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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