回到家里,看了一下第一个for内部的for完全是多余的,优化为:
1 List<int> configs = new List<int> { 1, 2, 1 }; 2 3 List<int> items = new List<int> { 4 1,2,3, 5 4,5,6, 6 7,8,9 7 }; 8 9 int skipCount = 0; 10 11 for (int i = 0; i < configs.Count - 1; i++) 12 { 13 int count = (int)(((decimal)configs[i] / configs.Sum()) * items.Count); 14 15 List<int> tempItems = items.Skip(skipCount).Take(count).ToList(); 16 17 Console.WriteLine("index:{0}-skip:{1}-take:{2}---------------------------", i, skipCount, count); 18 foreach (var tempItem in tempItems) 19 { 20 Console.WriteLine(tempItem); 21 } 22 Console.WriteLine("------------------------------------------------------"); 23 24 skipCount += count; 25 } 26 27 List<int> lastestItems = items.Skip(skipCount).Take(items.Count - skipCount).ToList(); 28 Console.WriteLine("index:{0}-skip:{1}-take:{2}---------------------------", configs.Count - 1, skipCount, items.Count - skipCount); 29 foreach (var lastestItem in lastestItems) 30 { 31 Console.WriteLine(lastestItem); 32 } 33 Console.WriteLine("------------------------------------------------------"); 34 35 Console.ReadKey();
运行结果: