CODEKILLER

반응형

Linq 그룹절(group)
Linq 그룹절(group)

using System.Xml.Linq;

public class Example
{
    public static void Main()
    {
        Console.Write("IEnumerable<T> 의 그룹절(group) 사용 \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" });
        customers.Add(new Customer() { City = "Busan", FirstName = "Dragon2", LastName = "Ball2" });
        customers.Add(new Customer() { City = "Busan", FirstName = "Dragon3", LastName = "Ball3" });

        var customerQuery =
            from cust in customers
            group cust by cust.City;

        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