从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