CODEKILLER

반응형

Linq 의 into 키워드의 사용
Linq 의 into 키워드의 사용

using System.Xml.Linq;

public class Example
{
    public static void Main()
    {
        Console.Write("IEnumerable<T> 의 사용 \n");

        List<Customer> customers = new List<Customer>();
        customers.Add(new Customer() { City = "Seoul", FirstName = "Code", LastName = "Killer" });
        customers.Add(new Customer() { City = "Seoul", FirstName = "Code1", LastName = "Killer1" });
        customers.Add(new Customer() { City = "Seoul", FirstName = "Code2", LastName = "Killer2" });
        customers.Add(new Customer() { City = "Seoul", FirstName = "Code3", LastName = "Killer3" });

        customers.Add(new Customer() { City = "Busan", FirstName = "Dragon", LastName = "Ball" });
        customers.Add(new Customer() { City = "Busan", FirstName = "Dragon1", LastName = "Ball1" });

        // group 된 정보를 다시 참조해야하는 경우에는 아래처럼
        // into 키워드를 사용하여 접근하면 됩니다.
        var customerQuery =
                from cust in customers
                group cust by cust.City into custGroup
                where custGroup.Count() > 2
                orderby custGroup.Key
                select custGroup;

        foreach (var customerGroup in customerQuery)
        {
            Console.WriteLine(customerGroup.Key);
            foreach (Customer customer in customerGroup)
            {
                Console.WriteLine("    {0}", customer.FirstName);
            }
        }
    }
}


public class Customer
{
    public string City { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band