解决Oracle in 超过1000个问题 C#拼接字符串

来源:互联网 发布:贵州中标数据网 编辑:程序博客网 时间:2024/05/21 13:22
 private string getOracleSQLIn(string[] ids, string field)
        {
            int count = Math.Min(ids.Length, 1000);
            int len = ids.Length;
            int size = len % count;
            if (size == 0)
            {
                size = len / count;
            }
            else
            {
                size = (len / count) + 1;
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < size; i++)
            {
                int fromIndex = i * count;
                int toIndex = Math.Min(fromIndex + count, len);
                string productId = string.Join("','", getArrayValues(fromIndex, toIndex, ids).ToArray());
                if (i != 0)
                {
                    builder.Append(" or ");
                }
                builder.Append(field).Append(" in ('").Append(productId).Append("')");
            }


            return builder.ToString();
        }




        public List<string> getArrayValues(int fromindex, int toindex, string[] array)
        {
            List<string> listret = new List<string>();
            for (int i = fromindex; i < toindex; i++)
            {
                listret.Add(array[i]);
            }
            return listret;
        }
0 0
原创粉丝点击