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

■ Enumerable 클래스의 Where/All 확장 메소드를 사용해 상대 리스트에 없는 항목을 추출하는 방법을 보여준다.

TestProject.zip
0.00MB

▶ Person.cs

namespace TestProject
{
    /// <summary>
    /// 사람
    /// </summary>
    public class Person
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region ID - ID

        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }

        #endregion
    }
}

 

▶ Program.cs

namespace TestProject;

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

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

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        List<Person> list1 = new List<Person>();

        list1.Add(new Person() { ID = 1 });
        list1.Add(new Person() { ID = 2 });
        list1.Add(new Person() { ID = 3 });
        list1.Add(new Person() { ID = 4 });
        list1.Add(new Person() { ID = 5 });

        List<Person> list2 = new List<Person>();

        list2.Add(new Person() { ID = 1 });
        list2.Add(new Person() { ID = 2 });
        list2.Add(new Person() { ID = 3 });

        // 리스트1에는 있지만 리스트2에는 없는 항목을 추출한다.
        IEnumerable<Person> personEnumerable = list1.Where(p1 => list2.All(p2 => p2.ID != p1.ID));

        foreach(Person person in personEnumerable)
        {
            Console.WriteLine(person.ID);
        }
    }

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

댓글을 달아 주세요