從list集合中數據以固定數目取出


假設一個接口請求數據每次最多只能10個,現在有105條數據怎么處理。
C#語言:

         List<int> list = new List<int>();
         //簡單制造數據
         for (int i = 1; i < 105;i++ ) 
         {
             list.Add(i);            
         }

         int listSize = list.Count();
         //每次最多請求數據個數
         int toIndex = 10;   

         for (int i = 0; i < list.Count(); i += 10)
         {
             if (i + 10 > listSize)
             {        
                 toIndex = listSize - i;
             }
             List<int> newList = new List<int>();
             /** java語言可以用subList()方法,C#中沒有看到,就用了下面的for循環方法 List<int> newList=list.sublist(i,i+toIndex); **/
             for (int j = i; j < i + toIndex;j++ ) 
             {
                 newList.Add(list[j]);
             }
             //這里newList已經拿到了10條數據,可以去做相應的業務
         }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM