■ IN 조건 문자열을 구하는 방법을 보여준다.
▶ IN 조건 문자열 구하기 예제 (C#)
1 2 3 4 5 6 7 8 |
using System; using System.Collections.Generic; string condition = GetINConditionString("ID", new List<string> { "A", "B", "C", "D", "E", "F", "G" }, 5); Console.WriteLine(condition); |
▶ IN 조건 문자열 구하기 (C#)
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
using System.Collections.Generic; using System.Text; #region IN 조건 문자열 구하기 - GetINConditionString(fieldName, valueList, itemGroupCount) /// <summary> /// IN 조건 문자열 구하기 /// </summary> /// <param name="fieldName">필드명</param> /// <param name="valueList">값 리스트</param> /// <param name="itemGroupCount">항목 그룹 수</param> /// <returns>IN 조건 문자열</returns> public string GetINConditionString(string fieldName, List<string> valueList, int itemGroupCount = 1000) { if(valueList == null || valueList.Count == 0) { return string.Empty; } int listCount = valueList.Count; int listGroupCount = (int)Math.Ceiling((double)listCount / itemGroupCount); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("AND ("); for(int i = 0; i < listGroupCount; i++) { int startIndex = i * itemGroupCount; int endIndex = (i + 1) * itemGroupCount - 1; if(endIndex > listCount - 1) { endIndex = listCount - 1; } if(i == 0) { AppendINConditionString(stringBuilder, fieldName, valueList, startIndex, endIndex); } else { stringBuilder.Append(" OR "); AppendINConditionString(stringBuilder, fieldName, valueList, startIndex, endIndex); } } stringBuilder.Append(")"); return stringBuilder.ToString(); } #endregion #region IN 조건 문자열 추가하기 - AppendINConditionString(stringBuilder, fieldName, valueList, startIndex, endIndex) /// <summary> /// IN 조건 문자열 추가하기 /// </summary> /// <param name="stringBuilder">문자열 빌더</param> /// <param name="fieldName">필드명</param> /// <param name="valueList">값 리스트</param> /// <param name="startIndex">시작 인덱스</param> /// <param name="endIndex">종료 인덱스</param> private void AppendINConditionString(StringBuilder stringBuilder, string fieldName, List<string> valueList, int startIndex, int endIndex) { stringBuilder.Append($"({fieldName} IN ("); for(int i = startIndex; i <= endIndex; i++) { stringBuilder.Append($"'{valueList[i]}',"); } stringBuilder.Remove(stringBuilder.Length - 1, 1); stringBuilder.Append("))"); } #endregion |