■ join ... on ... into 키워드 질의 형식 사용하기 (groupjoin 쿼리)

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

using System;

using System.Linq;

 

/// <summary>

/// 고객

/// </summary>

public class Customer

{

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

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

 

    #region 고객 ID - CustomerID

 

    /// <summary>

    /// 고객 ID

    /// </summary>

    public string CustomerID { get; set; }

 

    #endregion

 

    #region 고객명 - CustomerName

 

    /// <summary>

    /// 고객명

    /// </summary>

    public string CustomerName { get; set; }

 

    #endregion

}

 

/// <summary>

/// 주문

/// </summary>

public class Order

{

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

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

 

    #region 고객 ID - CustomerID

 

    /// <summary>

    /// 고객 ID

    /// </summary>

    public string CustomerID { get; set; }

 

    #endregion

 

    #region 주문명 - OrderName

 

    /// <summary>

    /// 주문명

    /// </summary>

    public string OrderName { get; set; }

 

    #endregion

}

 

...

 

var customerList = new List<Customer>

{

    new Customer { CustomerID = "P001", CustomerName= "고객1" },

    new Customer { CustomerID = "P002", CustomerName= "고객2" },

    new Customer { CustomerID = "P003", CustomerName= "고객3" },

    new Customer { CustomerID = "P004", CustomerName= "고객4" },

};

 

var orderList = new List<Order>

{

    new Order { CustomerID = "P001", OrderName = "O001" },

    new Order { CustomerID = "P004", OrderName = "O002" },

    new Order { CustomerID = "P001", OrderName = "O003" },

    new Order { CustomerID = "P001", OrderName = "O004" },

    new Order { CustomerID = "P003", OrderName = "O005" },

};

 

var resultEnumerable = from customer in customerList

                       join order    in orderList

                       on   customer.CustomerID equals order.CustomerID into customerOrderList

                       select new

                       {

                           CustomerName      = customer.CustomerName,

                           CustomerOrderList = customerOrderList

                       };

 

foreach(var result in resultEnumerable)

{

    Console.WriteLine("고객명 : {0}", result.CustomerName);

 

    foreach(var customerOrder in result.CustomerOrderList)

    {

        Console.WriteLine("  주문번호 : {0}", customerOrder.OrderName);

    }

}

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

Posted by 사용자 icodebroker
TAG