C# - 按指定項目數量分割數組,oracle in表達式參數支持最大上限1000個替代方案

C# – 按指定項目數量分割數組,oracle in表達式參數支持最大上限1000個替代方案
c# - 分割數組的最佳方法[關閉]

C# 按指定項目數量分割數組

以下 是對項目數量爲1200的數字items,每100個項目數量爲一組分割,用二位數字chunks存放分組後的數組。

輸出結果查看:IDE在線

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在線

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();
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章