使用 foreach 循环提取 List<Class> 中相同编号的前 10 个条目

我有一个类型的集合NumberList。


public class NumberList

{

    public int Number { get; set; }

    public double Profit { get; set; }

    public DateTime CloseTime { get; set; }

}

所有记录都存储在NumberList中,并按 Number 和 Date 降序排序。


现在我需要循环遍历整个列表,搜索有 10 个条目的 Number 并将它们存储在新列表中。


我遇到的问题是我得到了 10 个值,但不是相同的数字,比如数字 26724 有 9 个条目,他取下一个 18450 并将其也添加到列表中,因为它缺少 1。它不需要添加其他数字,而是需要重置并tempList继续在下一个数字上,因为它只有 9 个条目。


我如何提取相同编号的前 10 个条目并将它们添加到新列表中?


这是代码:


var tempList = new List<NumberList>(); // temporary list

var finalList = new List<NumberList>(); // new list where we store final results


foreach (var item in NumberList)

{

    tempList.Add(item); // add numbers to temp list


    // if temlist contains Number and number of items in list is less then 10

    if (tempList.Contains(item) && tempList.Count() < 10)

    {

        finalList.Add(item);

    }

    else

    {

        if (tempList.Count() == 10) // check for number of items in list

        {

            tempList.Clear(); // reset tempList

        }

    }

}

例如,它需要如何工作:号码 26724 只有 9 个条目,因此他不会出现在新列表中,号码 18450 有 8 个条目,他不会出现在列表中,号码 16822 有 20 个条目,因此我们取出该号码的前 10 个条目并放入在新列表中。


26724, -6.55, 18-Jul-19 08:32:30

26724, 12.21, 20-Jun-19 03:54:56

26724, -6.53, 14-Jun-19 20:09:28

26724, 12.15, 31-May-19 17:13:25

26724, 0.98, 21-May-19 09:00:01

26724, 4.21, 15-May-19 07:02:02

26724, -6.56, 08-May-19 18:00:43

26724, -7.35, 24-Apr-19 18:40:25

26724, -6.59, 04-Apr-19 21:08:40

18450, 6.79, 18-Jul-19 22:16:26

18450, 6.69, 20-Jun-19 03:31:27

18450, 6.82, 14-Jun-19 09:57:16

18450, 6.66, 31-May-19 20:27:05

18450, -0.28, 13-May-19 15:59:08

18450, -5.95, 08-May-19 18:00:01

18450, -3.53, 24-Apr-19 12:00:42

18450, -6.05, 04-Apr-19 21:00:03

16822, 10.38, 11-Jul-19 04:56:27

16822, 9.88, 27-Jun-19 09:00:00

16822, 0.43, 17-Jun-19 16:00:02

16822, -2.36, 11-Jun-19 04:00:00

16822, -9.82, 05-Jun-19 20:08:02

16822, 13.31, 31-May-19 21:06:21

16822, 1.49, 22-May-19 10:00:02

16822, -2.8, 17-May-19 12:00:01

16822, -8.8, 13-May-19 15:07:46

16822, -8.43, 10-May-19 21:49:31

16822, -5.84, 03-May-19 16:45:26

...... etc



慕哥6287543
浏览 167回答 5
5回答

ibeautiful

现在我需要循环遍历整个列表,搜索有 10 个条目的 Number 并将它们存储在新列表中。Foeach 不太适合做这项工作。您需要 2 个 For 循环。一个用于每个(不同的)项目编号,一个用于比较。由于您的集合是有序的,因此您可以使用它来避免对基本集合进行多次迭代。每次新系列开始时,您都可以记住内循环的索引结束并在外循环中跳转到记住的位置:for (int i = 0; i < NumberList.Count; i++){&nbsp; &nbsp; tempList.Clear();&nbsp; &nbsp; for (int j = i+1; j < NumberList.Count; j++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (NumberList[i].Number == NumberList[j].Number)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempList.Add(NumberList[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Note that a new series of numbers has began and jump to this position&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; // end this counting procedure&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // at this point evalueate the counter&nbsp; &nbsp; if (tempList.Count >= 10)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; finalList.AddRange(tempList.Take(10));&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}简短的 Linq 解决方案可能如下所示:NumberList.GroupBy(x => x.Number).Where(x => x.Count() >= 10).SelectMany(x => x.Take(10));只需将具有相同值的所有数字收集到分组集合中即可。然后对其应用过滤器,检查哪一个符合您出现 10 次或以上的标准。然后仅选择这些项目/取消它们的分组并仅选取前 10 个;

翻阅古今

您可以使用 Linq 来执行此操作。例如,var&nbsp;finalList&nbsp;=&nbsp;numberList &nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(a&nbsp;=>&nbsp;a.Number) &nbsp;&nbsp;&nbsp;&nbsp;.Where(a&nbsp;=>&nbsp;a.Count()&nbsp;>=&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(a&nbsp;=>&nbsp;a.OrderBy(b&nbsp;=>&nbsp;b.CloseTime).Take(10)) &nbsp;&nbsp;&nbsp;&nbsp;.ToList();所以首先我们使用 来按数字分组GroupBy。然后,我们使用 限制仅包含 10 个或更多条目的分组Where(a => a.Count() >= 10)。然后我们使用SelectMany来展平分组,并使用 来选择每个分组的前 10 个元素Take,并使用OrderBy来确保它们的顺序一致。

qq_花开花谢_0

NumberList &nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(x&nbsp;=>&nbsp;x.Number) &nbsp;&nbsp;&nbsp;&nbsp;.Where(groupings&nbsp;=>&nbsp;grouping.Count()&nbsp;>&nbsp;10)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(groupings&nbsp;=>&nbsp;groupings) &nbsp;&nbsp;&nbsp;&nbsp;.ToList()

米琪卡哇伊

var&nbsp;tempList&nbsp;=&nbsp;new&nbsp;List<NumberList>();&nbsp;//&nbsp;temporary&nbsp;list var&nbsp;finalList&nbsp;=&nbsp;tempList.GroupBy(t&nbsp;=>&nbsp;t.Number) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(t&nbsp;=>&nbsp;t.Count()&nbsp;>=&nbsp;10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(t&nbsp;=>&nbsp;t.Take(10))&nbsp;//take&nbsp;10&nbsp;elements&nbsp;from&nbsp;filtered&nbsp;data &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList();

四季花海

您可以在 Number 字段上创建一个不同的值,并使用不同的结果循环该对象&nbsp;foreach (var item in numList.Select(x=> x.Number).Distinct())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(numList.Where(x=> x.Number.Equals(item)).Count() >= 10 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( var item2 in numList.Where(x=> x.Number.Equals(item)) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(counter <10 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalList.Add(item2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach(var test in finalList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(string.Format("{0}, {1}", test.Number, test.Profit));
打开App,查看更多内容
随时随地看视频慕课网APP