【问题标题】:List elements collection列表元素集合
【发布时间】:2015-06-16 08:41:37
【问题描述】:

我有一份清单:

List<string> list = new List<string>();
list.Add("x1");
list.Add("x2");
list.Add("x3");
list.Add("x4");
.
.
.
.
list.Add("x100");

现在我需要一个 List 集合,其中正好包含来自 listofX 的 10 个元素。

所以 List1 有来自 listofX 的 1 到 10 个元素,list2 有来自 listofX 的 11 到 20 个元素。

这可以用 Lambda 表达式或 LINQ 来完成吗

【问题讨论】:

    标签: c# linq list collections lambda


    【解决方案1】:

    可以这样做:

    var list1 = list.Take(10).ToList();
    var list2 = list.Skip(10).Take(10).ToList();
    var list3 = list.Skip(20).Take(10).ToList();
    

    等等

    或更笼统地说:

    var number = list.Count / 10;
    var lists = new List<List<string>>(number);
    
    for (int i = 0; i < number; i++)
        lists.Add(list.Skip(i * 10).Take(10));
    

    这将创建这些列表的列表。

    【讨论】:

    • 非常优雅和简单。不错的一个
    【解决方案2】:

    类似:

    var lists = Enumerable
        .Range(0,10)
        .Select(x=>
            Enumerable
                .Range(x*10+1,10)
                .Select(y=>"x" + y.ToString()));
    

    如果您正在寻找通用方法,那么也许...

     var list=Enumerable.Range(1,100).Select(y=>"x" + y.ToString());
     var newlist=Enumerable.Range(0,10).Select(x=>list.Skip(x*10).Take(10));
    

    或者最后,你可以使用这样的扩展方法:

        public static IEnumerable<List<T>> InSetsOf<T>(this IEnumerable<T> source, int max)
        {
            var toReturn = new List<T>(max);
            foreach (var item in source)
            {
                toReturn.Add(item);
                if (toReturn.Count == max)
                {
                    yield return toReturn;
                    toReturn = new List<T>(max);
                }
            }
            if (toReturn.Any())
            {
                yield return toReturn;
            }
        }
    

    你可以这样使用它:

    var newList=list.InSetsOf(10);
    

    【讨论】:

    • 第二个范围应该是skip和take的组合,因为“x1”可能是一个占位符。
    • 如果是占位符,那么是的。它需要重新设计,我可能会使用不同的方法。
    【解决方案3】:

    您正在寻找的是一种批处理列表的方法,编写一个 IEnumerable 扩展来实现这一点相当简单。

    以下是您可以按原样使用的代码:

    public static class Extensions      {
    public static IEnumerable<IEnumerable<TSource>> BreakIntoChunks<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;
    
                        // The bucket is fully buffered before it's yielded
                        if (count != size)
                        {
                            continue;
                        }
    
                        // Select is necessary so bucket contents are streamed too
                        yield return bucket.Select(x => x);
    
                        bucket = null;
                        count = 0;
                    }
    
                    // Return the last bucket with all remaining elements
                    if (bucket != null && count > 0)
                    {
                        yield return bucket.Take(count);
                    }
                }
    }
    

    使用此方法,您可以简单地调用list.BreakIntoChunks(10);//分成10个项目的批次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2020-04-12
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多