第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 foreach 循環(huán)提取 List<Class> 中相同編號的前 10 個條目

使用 foreach 循環(huán)提取 List<Class> 中相同編號的前 10 個條目

C#
慕哥6287543 2023-07-09 17:44:11
我有一個類型的集合NumberList。public class NumberList{    public int Number { get; set; }    public double Profit { get; set; }    public DateTime CloseTime { get; set; }}所有記錄都存儲在NumberList中,并按 Number 和 Date 降序排序?,F(xiàn)在我需要循環(huán)遍歷整個列表,搜索有 10 個條目的 Number 并將它們存儲在新列表中。我遇到的問題是我得到了 10 個值,但不是相同的數(shù)字,比如數(shù)字 26724 有 9 個條目,他取下一個 18450 并將其也添加到列表中,因為它缺少 1。它不需要添加其他數(shù)字,而是需要重置并tempList繼續(xù)在下一個數(shù)字上,因為它只有 9 個條目。我如何提取相同編號的前 10 個條目并將它們添加到新列表中?這是代碼:var tempList = new List<NumberList>(); // temporary listvar finalList = new List<NumberList>(); // new list where we store final resultsforeach (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 個條目,因此他不會出現(xiàn)在新列表中,號碼 18450 有 8 個條目,他不會出現(xiàn)在列表中,號碼 16822 有 20 個條目,因此我們?nèi)〕鲈撎柎a的前 10 個條目并放入在新列表中。26724, -6.55, 18-Jul-19 08:32:3026724, 12.21, 20-Jun-19 03:54:5626724, -6.53, 14-Jun-19 20:09:2826724, 12.15, 31-May-19 17:13:2526724, 0.98, 21-May-19 09:00:0126724, 4.21, 15-May-19 07:02:0226724, -6.56, 08-May-19 18:00:4326724, -7.35, 24-Apr-19 18:40:2526724, -6.59, 04-Apr-19 21:08:4018450, 6.79, 18-Jul-19 22:16:2618450, 6.69, 20-Jun-19 03:31:2718450, 6.82, 14-Jun-19 09:57:1618450, 6.66, 31-May-19 20:27:0518450, -0.28, 13-May-19 15:59:0818450, -5.95, 08-May-19 18:00:0118450, -3.53, 24-Apr-19 12:00:4218450, -6.05, 04-Apr-19 21:00:0316822, 10.38, 11-Jul-19 04:56:2716822, 9.88, 27-Jun-19 09:00:0016822, 0.43, 17-Jun-19 16:00:0216822, -2.36, 11-Jun-19 04:00:0016822, -9.82, 05-Jun-19 20:08:0216822, 13.31, 31-May-19 21:06:2116822, 1.49, 22-May-19 10:00:0216822, -2.8, 17-May-19 12:00:0116822, -8.8, 13-May-19 15:07:4616822, -8.43, 10-May-19 21:49:3116822, -5.84, 03-May-19 16:45:26...... etc
查看完整描述

5 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

現(xiàn)在我需要循環(huán)遍歷整個列表,搜索有 10 個條目的 Number 并將它們存儲在新列表中。


Foeach 不太適合做這項工作。您需要 2 個 For 循環(huán)。一個用于每個(不同的)項目編號,一個用于比較。由于您的集合是有序的,因此您可以使用它來避免對基本集合進行多次迭代。每次新系列開始時,您都可以記住內(nèi)循環(huán)的索引結束并在外循環(huán)中跳轉到記住的位置:


for (int i = 0; i < NumberList.Count; i++)

{

    tempList.Clear();

    for (int j = i+1; j < NumberList.Count; j++)

    {

        if (NumberList[i].Number == NumberList[j].Number)

        {

            tempList.Add(NumberList[i]);                

        }

        else

        {

            // Note that a new series of numbers has began and jump to this position

            i = j;

            break; // end this counting procedure

        }

    }


    // at this point evalueate the counter

    if (tempList.Count >= 10)

    {

        finalList.AddRange(tempList.Take(10));

    }       

}

簡短的 Linq 解決方案可能如下所示:


NumberList.GroupBy(x => x.Number).Where(x => x.Count() >= 10).SelectMany(x => x.Take(10));

只需將具有相同值的所有數(shù)字收集到分組集合中即可。

然后對其應用過濾器,檢查哪一個符合您出現(xiàn) 10 次或以上的標準。

然后僅選擇這些項目/取消它們的分組并僅選取前 10 個;


查看完整回答
反對 回復 2023-07-09
?
翻閱古今

TA貢獻1780條經(jīng)驗 獲得超5個贊

您可以使用 Linq 來執(zhí)行此操作。例如,

var finalList = numberList
    .GroupBy(a => a.Number)
    .Where(a => a.Count() >= 10)
    .SelectMany(a => a.OrderBy(b => b.CloseTime).Take(10))
    .ToList();

所以首先我們使用 來按數(shù)字分組GroupBy。然后,我們使用 限制僅包含 10 個或更多條目的分組Where(a => a.Count() >= 10)。然后我們使用SelectMany來展平分組,并使用 來選擇每個分組的前 10 個元素Take,并使用OrderBy來確保它們的順序一致。


查看完整回答
反對 回復 2023-07-09
?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

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


查看完整回答
反對 回復 2023-07-09
?
米琪卡哇伊

TA貢獻1998條經(jīng)驗 獲得超6個贊

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


查看完整回答
反對 回復 2023-07-09
?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

您可以在 Number 字段上創(chuàng)建一個不同的值,并使用不同的結果循環(huán)該對象


 foreach (var item in numList.Select(x=> x.Number).Distinct())

        {

             int counter = 0;

              if(numList.Where(x=> x.Number.Equals(item)).Count() >= 10 )

              { 

                  foreach( var item2 in numList.Where(x=> x.Number.Equals(item)) ){

                      if(counter <10 ) {

                          finalList.Add(item2);

                          counter ++;

                      }

                  }

              }

        }

        foreach(var test in finalList)

            Console.WriteLine(string.Format("{0}, {1}", test.Number, test.Profit));


查看完整回答
反對 回復 2023-07-09
  • 5 回答
  • 0 關注
  • 278 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號