■ Expression 클래스 : 객체 컬렉션 특정 객체 속성 값의 집합 구하기 (속성 1개만 지정)

----------------------------------------------------------------------------------------------------

Program.cs

 

using System;

using System.ComponentModel;

using System.Collections.ObjectModel;

using System.Linq;

using System.Linq.Expressions;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    public class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            ObservableCollection<Employee> collection = Employee.GetCollection();

 

            string propertyName = "Sex";

 

            ParameterExpression parameterExpression = Expression.Parameter(typeof(Employee), "p");

            MemberExpression    memberExpression    = Expression.Property(parameterExpression, propertyName);

            LambdaExpression    lambdaExpression    = Expression.Lambda(memberExpression, parameterExpression);

            Delegate            function            = lambdaExpression.Compile();

 

            var result =

            (

                from    item in collection

                group   item by function.DynamicInvoke(item)

                into    group1

                select  group1.First().GetType().GetProperty(propertyName).GetValue(group1.First()).ToString()

                into    group2

                orderby group2

                select  group2

            ).ToList();

 

            foreach(var item in result)

            {

                Console.WriteLine(item);

            }

        }

 

        #endregion

    }

}

 

Employee.cs

 

using System.Collections.ObjectModel;

 

namespace TestProject

{

    /// <summary>

    /// 직원

    /// </summary>

    public class Employee

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region ID - ID

 

        /// <summary>

        /// ID

        /// </summary>

        public string ID { get; set; }

 

        #endregion

 

        #region 성명 - Name

 

        /// <summary>

        /// 성명

        /// </summary>

        public string Name { get; set; }

 

        #endregion

 

        #region 성별 - Sex

 

        /// <summary>

        /// 성별

        /// </summary>

        public string Sex { get; set; }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 컬렉션 구하기 - GetCollection()

 

        /// <summary>

        /// 컬렉션 구하기

        /// </summary>

        /// <returns>컬렉션</returns>

        public static ObservableCollection<Employee> GetCollection()

        {

            ObservableCollection<Employee> collection = new ObservableCollection<Employee>();

 

            for(int i = 0; i < 100000; i++)

            {

                collection.Add(new Employee() { ID = i.ToString(), Name = "직원" + i.ToString(), Sex = (i % 3 == 1 ? "남" : "여")});

            }

 

            return collection;

        }

 

        #endregion

    }

}

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG