■ LINQ 확장 메소드를 사용해 각 그룹에서 첫번째 항목만 출력하는 방법을 보여준다.
▶ Item.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 54 55 56 57 58 59 60 61 |
namespace TestProject; /// <summary> /// 항목 /// </summary> public class Item { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion #region 연도 - Year /// <summary> /// 연도 /// </summary> public int Year { get; set; } #endregion #region 데이터 - Data /// <summary> /// 데이터 /// </summary> public int Data { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"{ID}, {Name}, {Year}, {Data}"; } #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 54 55 56 57 58 59 60 61 62 63 64 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<Item> sourceList = new List<Item> { new Item { ID = 1, Name = "Nima", Year = 1990, Data = 10 }, new Item { ID = 2, Name = "Nima", Year = 1990, Data = 11 }, new Item { ID = 3, Name = "Nima", Year = 2000, Data = 12 }, new Item { ID = 4, Name = "John", Year = 2001, Data = 1 }, new Item { ID = 5, Name = "John", Year = 2002, Data = 2 }, new Item { ID = 6, Name = "Sara", Year = 2010, Data = 4 } }; Console.WriteLine("소스 리스트"); Console.WriteLine("----------------------------------------"); foreach(Item item in sourceList) { Console.WriteLine(item); } Console.WriteLine("----------------------------------------"); Console.WriteLine(); Console.WriteLine("결과 리스트"); Console.WriteLine("----------------------------------------"); IEnumerable<Item> resultEnumerable = sourceList.GroupBy(x => x.Name) .Select ( groups => new Item { Name = groups.First().Name, Year = groups.First().Year, Data = groups.First().Data } ); foreach(var item in resultEnumerable) { Console.WriteLine(item); } Console.WriteLine("----------------------------------------"); } #endregion } |