첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ 리스트 분리하기 예제

List<int> sourceList = new List<int> { 5, 10, 10, 20, 30, 100, 50, 20, 30, 40, 50 };

List<List<int>> targetListList = SplitList<int>(sourceList, 3, 9);

foreach(List<int> targetList in targetListList)
{
    foreach(int target in targetList)
    {
        Console.Write(target);
        Console.Write(" "   );
    }

    Console.WriteLine();
}

 

728x90

 

▶ 리스트 분리하기

#region 리스트 분리하기 - SplitList<T>(sourceEnumerable, groupSize, maximumCount)

/// <summary>
/// 리스트 분리하기
/// </summary>
/// <typeparam name="TElement">요소 타입</typeparam>
/// <param name="sourceEnumerable">소스 열거 가능형</param>
/// <param name="groupSize">그룹 크기</param>
/// <param name="maximumCount">최대 카운트</param>
/// <returns>분리 리스트 리스트</returns>
public List<List<TElement>> SplitList<TElement>(IEnumerable<TElement> sourceEnumerable, int groupSize, int? maximumCount = null)
{
    List<List<TElement>> targetListList = new List<List<TElement>>();

    if(sourceEnumerable.Count() <= groupSize)
    {
        targetListList.Add(sourceEnumerable.ToList());
    }
    else
    {
        List<TElement> sourceList = sourceEnumerable.ToList();

        int startIndex   = 0;
        int count        = sourceList.Count;
        int elementCount = 0;

        while(startIndex < count && (!maximumCount.HasValue || (maximumCount.HasValue && startIndex < maximumCount)))
        {
            elementCount = (startIndex + groupSize > count) ? count - startIndex : groupSize;

            targetListList.Add(sourceList.GetRange(startIndex, elementCount));

            startIndex += elementCount;
        }
    }

    return targetListList;
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요