以下 是对项目数量为1200的数字items,每100个项目数量为一组分割,用二位数字chunks存放分组后的数组。
输出结果查看:IDE在线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Linq; public class Test { public static void Main() { // 生成 1200 个字串的数组 string[] items = Enumerable.Range(1, 1200).Select(i => "Item" + i).ToArray(); // 每100个项目数分组。用items的index/100作为分组依据。 String[][] chunks = items .Select((s, i) => new { Value = s, Index = i }) .GroupBy(x => x.Index / 100) .Select(grp => grp.Select(x => x.Value).ToArray()) .ToArray(); for (int i = 0; i < chunks.Length; i++) { foreach (var item in chunks[i]) Console.WriteLine("chunk:{0} {1}", i, item); } } } |
实例应用:oracle in表达式参数支持最大上限1000个替代方案是每1000个放在一个in中
实例测试:IDE在线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; class Program { static void Main(string[] args) { string sqlInCol = "ima01 in "; //string[] dataList = System.IO.File.ReadAllLines( "../../../DataList.txt"); string[] dataList ={ "220000004754", "220000004755", "220000004814", "220000004817", "220000004836", "220000004877", "220000004878", "220000004879", "220000004980", "220000004984", "220000004988" }; String[] chunks = dataList .Select((s, i) => new { Value = s, Index = i }) .GroupBy(x => x.Index / 2)//测试一组2个。 .Select(grp => ($"( '{string.Join("','", grp.Select(x => x.Value).ToArray())}' )\r\n")) .ToArray(); string sqlIn = sqlInCol +string.Join($"or {sqlInCol }", chunks); Console.WriteLine(sqlIn); Console.ReadLine(); } } |