如何制作具有共同项目的现有列表的子列表? [英] How to make sublist of existing list having common items?

查看:78
本文介绍了如何制作具有共同项目的现有列表的子列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2,30,000项目的清单(A),其中列表中的第1项是(3,1110000000)这里这个例子中3是数字1110000000的总和。我按照总和按照降序对这个列表进行了排序,现在我的第一项是(57,9657551955)。现在有许多数字,其总和为57,依此类推,然后将总和为57的所有数字转储到名为57.CSV的文件中,依此类推。现在按降序对列表进行排序后,我发现从第0个索引到第121个索引的总和为57,从第122个索引到第178个索引的总和为56。现在我想从列表(A)创建所有总和57,56等的列表。怎么做?



请建议。



我的尝试:



I have list(A) of 2,30,000 items where 1st item in list is (3,1110000000) here in this example 3 is sum of number 1110000000. and I sorted this list in descending order according to sum and now my 1st item is (57,9657551955). now there are many numbers whose sum is 57 and so on then dump all numbers whose sum is 57 into a file named "57.CSV" and so on. now after sorting the list in descending order I found 57 sum from 0th index to 121th index , 56 sum from 122th index to 178th index. Now i want to create lists from list(A) for all sums 57 , 56 and so on. How to do this?

Please suggest.

What I have tried:

using (StreamReader sr = new StreamReader(fs))
                    {
                        string barcode_data = sr.ReadToEnd().Replace("\r\n", ",");
                        string[] Allbarcodes = barcode_data.Split(',');
                        int counter = 0;

                        for (int i = 0; i < Allbarcodes.Length - 1; i++)
                        {
                            if (!string.IsNullOrEmpty(Allbarcodes[i]))
                            {
                                long sumOfnum = Summation(Convert.ToInt64(Allbarcodes[i]));
                                barcodeList.Add(sumOfnum + "," + Allbarcodes[i]);
                            }
                        }
                        Array.Clear(Allbarcodes, 0, Allbarcodes.Length);
                        label1.Text = counter++.ToString();

                        result = barcodeList.Select(m => new { value = int.Parse(m.Substring(0, m.IndexOf(','))), item = m }).
                                     OrderByDescending(m => m.value).Select(m => m.item).ToList();
////list result contains all items and have to make sublists of it//// 
                        sr.Close();
                        sr.Dispose();
                    }

推荐答案

看看使用GroupBy: Enumerable.GroupBy(TSource,TKey,TElement)方法(IEnumerable(TSource),Func(TSource,TKey),Func(TSource,TElement))(系统.Linq) [ ^ ]





[edit]

您可能还会觉得这很有用: 使用Linq创建子列表字典通过从集合中分组。 [ ^ ]

[/ edit]
Look at using GroupBy: Enumerable.GroupBy(TSource, TKey, TElement) Method (IEnumerable(TSource), Func(TSource, TKey), Func(TSource, TElement)) (System.Linq)[^]


[edit]
You may also find this useful: Using Linq to create a Dictionary of sub-Lists by grouping from a collection.[^]
[/edit]


Linq的 GroupBy 我认为会做你想做的事。

如何:分组查询结果(C#编程指南) [ ^ ]
Linq's GroupBy I think would do what you want.
How to: Group Query Results (C# Programming Guide)[^]


这篇关于如何制作具有共同项目的现有列表的子列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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