■ Enumerable 클래스의 GroupBy<TSource, TKey> 확장 메소드를 사용하는 방법을 보여준다.
▶ Product.cs
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 |
namespace TestProject; /// <summary> /// 제품 /// </summary> public class Product { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 코드 - ProductCode /// <summary> /// 제품 코드 /// </summary> public string ProductCode { get; set; } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 가격 - Price /// <summary> /// 가격 /// </summary> public decimal Price { get; set; } #endregion } |
▶ ProductSummary.cs
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 |
namespace TestProject; /// <summary> /// 제품 요약 /// </summary> public class ProductSummary { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품명 - ProductName /// <summary> /// 제품명 /// </summary> public string ProductName { get; set; } #endregion #region 금액 - Amount /// <summary> /// 금액 /// </summary> public string Amount { get; set; } #endregion #region 수량 - Quantity /// <summary> /// 수량 /// </summary> public string Quantity { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"{ProductName} | {Amount} | {Quantity}"; } #endregion } |
▶ Program.cs
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 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Random random = new Random(); List<Product> sourceList = new List<Product>(); for(int i = 0; i < 100; i++) { int value = random.Next(1, 10); sourceList.Add(new Product { ProductCode = $"제품코드 {value}", Name = $"제품명 {value}", Price = random.Next(1000, 10000) }); } List<ProductSummary> targetList = sourceList .GroupBy(source => source.ProductCode) .Select ( sourceGroup => new ProductSummary { ProductName = sourceGroup.First().Name, Quantity = sourceGroup.Count().ToString(), Amount = sourceGroup.Sum(c => c.Price).ToString(), } ) .OrderBy(source => source.ProductName) .ToList(); foreach(ProductSummary summary in targetList) { Console.WriteLine(summary); } } #endregion } |