Chủ Nhật, 15 tháng 2, 2009

Làm thế nào để sắp xếp các kết quả của một Clause join

Ví dụ này cho thấy làm thế nào để sắp xếp các kết quả của một hoạt động join. Lưu ý rằng các lệnh được thực hiện sau khi join. Mặc dù bạn có thể sử dụng một mệnh đề orderby với một hoặc nhiều trình tự mã nguồn trước khi join, nhìn chung chúng takhông giới thiệu nó. Một số nhà cung cấp LINQ có thể không có duy trì lệnh sau khi join.

Truy vấn này tạo ra một group join, và sau đó sắp xếp các group dựa trên các yếu tố thể loại, mà vẫn còn trong phạm vi. Bên trong bộ khởi tạo kiểu anonymous, một sub query orhers tất cả các yếu tố kết hợp từ các chuỗi sản phẩm.

class HowToOrderJoins
{
       #region Data
       class Product
       {
              public string Name { get; set; }
              public int CategoryID { get; set; }
       }

       class Category
       {
              public string Name { get; set; }
              public int ID { get; set; }
       }

       // Specify the first data source.
       List categories = new List()
       {
               new Category(){Name="Beverages", ID=001},
               new Category(){ Name="Condiments", ID=002},
               new Category(){ Name="Vegetables", ID=003},
               new Category() { Name="Grains", ID=004},
               new Category() { Name="Fruit", ID=005}
       };

       // Specify the second data source.
       List products = new List()
       {
               new Product{Name="Cola", CategoryID=001},
               new Product{Name="Tea", CategoryID=001},
               new Product{Name="Mustard", CategoryID=002},
               new Product{Name="Pickles", CategoryID=002},
               new Product{Name="Carrots", CategoryID=003},
               new Product{Name="Bok Choy", CategoryID=003},
               new Product{Name="Peaches", CategoryID=005},
               new Product{Name="Melons", CategoryID=005},
       };
       #endregion
       
       static void Main()
       {
               HowToOrderJoins app = new HowToOrderJoins();
               app.OrderJoin1();

               // Keep console window open in debug mode.
               Console.WriteLine("Press any key to exit.");
               Console.ReadKey();
        }

        void OrderJoin1()
        {
                var groupJoinQuery2 =  from category in categories
                       join prod in products on category.ID equals prod.CategoryID into prodGroup
                       orderby category.Name
                       select new
                       {
                               Category = category.Name,
                               Products = from prod2 in prodGroup  orderby prod2.Name
                               select prod2
                       };
                foreach (var productGroup in groupJoinQuery2)
                {
                        Console.WriteLine(productGroup.Category);
                        foreach (var prodItem in productGroup.Products)
                        {
                                Console.WriteLine(" {0,-10} {1}", prodItem.Name, prodItem.CategoryID);
                         }
                 }
         }
        /* Output :
            Beverages
            Cola 1
            Tea 1
            Condiments
            Mustard 2
            Pickles 2
            Fruit
            Melons 5
            Peaches 5
            Grains
            Vegetables
            Bok Choy 3
            Carrots 3
         */
}
DangTrung.

Không có nhận xét nào:

Đăng nhận xét