次の方法で共有


How to: Order the Results of a Join Clause (C# Programming Guide)

This example shows how to order the results of a join operation. Note that the ordering is performed after the join. Although you can use an orderby clause with one or more of the source sequences before the join, generally we do not recommend it. Some LINQ providers might not preserve that ordering after the join.

Example

This query creates a group join, and then sorts the groups based on the category element, which is still in scope. Inside the anonymous type initializer, a sub-query orders all the matching elements from the products sequence.

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<Category> categories = new List<Category>()
 { 
     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<Product> products = new List<Product>()
{
   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
          */
     }

Compiling the Code

  • Create a Visual Studio project that targets the .NET Framework version 3.5. By default, the project has a reference to System.Core.dll and a using directive for the System.Linq namespace.

  • Copy the code into your project.

  • Press F5 to compile and run the program.

  • Press any key to exit the console window.

See Also

Reference

orderby clause (C# Reference)

join clause (C# Reference)

Concepts

LINQ Query Expressions (C# Programming Guide)

Join Operations