재우니의 블로그

C# : LINQ GROUPBY 활용 - 10개씩 묶음 만들어 출력하기

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
	internal class Program
	{
		public static void Main()
		{
			
			var modelList = new List<Moon>();

            for (int i = 1; i <= 22; i++)
            {
                modelList.Add(new Moon()
                {
                    idx = i,
                    name = System.Guid.NewGuid().ToString()
                });
            }

            List<List<Moon>> groupsOf10 = modelList
				.Select((str, index) => new { str, index })
				.GroupBy(x => x.index / 10)
				.Select(g => g.Select(x => x.str).ToList())
				.ToList();

			foreach(var groupInfo in groupsOf10)
            {
				foreach(var info in groupInfo)
                {
					Console.WriteLine($"{info.idx} {info.name}");
                }

				Console.WriteLine("============================");
			}

			Console.ReadKey();
		}


	}


	public class Moon{
		public int idx { get; set; } = 0;
		public string name { get; set; } = "";
    }
}

 

 

 

1 f1504112-225d-4eb9-89cc-203d46fb32a3
2 55a6033b-733a-44e2-ba5c-f504a7e39b75
3 b161b563-9cf2-4659-8de3-3faf4d598b6a
4 451d8ce1-0ceb-439d-9acf-a9290dde43bf
5 e29de3d0-5450-40cb-851e-c89ff06c9f5e
6 7bc5dfa7-1450-4411-831f-78eef4082747
7 8d556c07-b082-4666-9234-99df12b8b1b8
8 7d516027-7f71-45d4-93b8-75b615a967dd
9 8bc09605-b25b-4a12-b8fe-84c08491d81d
10 01e5a464-5168-473c-b902-81919b8e9771
============================
11 d4f849c2-9d0c-4baf-9816-e33a10091461
12 63e33bb2-04ac-4f30-a6af-3922455fd282
13 dbd67c5d-075e-4fcf-b50b-2bcb2d29de27
14 40d3116a-44fa-4c7b-8422-acc923e6fff7
15 bbd3aa0d-5313-439c-ac7f-ed9983f0b614
16 6d85c55f-42ef-4131-974f-770b95b71715
17 d648c218-2030-469c-a4c2-ed7219417f10
18 79446edf-8ea5-4afe-b2ca-a7abae354a7d
19 2a90a881-6946-42d1-92ca-f03fb1f5465f
20 7931ae69-99c7-4e67-8d4c-aa4a58ec9931
============================
21 0eabc943-e7d0-4f4d-a9f1-fd33a61c80ed
22 0c6ca232-2330-4616-b493-f83840a5bf20
============================

 

소스코드.txt
0.00MB