方法 : join 句の結果の順序を指定する (C# プログラミング ガイド)

更新 : 2007 年 11 月

この例は、結合操作の結果の順序を指定する方法を示しています。順序付けは結合後に行われます。結合の前に 1 つ以上のソース シーケンスを指定した orderby 句を使用することもできますが、一般にこの方法は推奨されません。一部の LINQ プロバイダは、結合後にその順序付けを維持しない可能性があります。

使用例

このクエリは、グループ結合を作成し、次にまだスコープ内にあるカテゴリ要素に基づいて各グループを並べ替えます。匿名型初期化子の内部では、結果のシーケンス内の一致するすべての要素がサブクエリによって順序付けられます。

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
     */
}

コードのコンパイル方法

  • .NET Framework Version 3.5 を対象とする Visual Studio プロジェクトを作成します。既定では、プロジェクトには System.Core.dll への参照と System.Linq 名前空間に対する using ディレクティブが含まれます。

  • コードをプロジェクト内にコピーします。

  • F5 キーを押して、プログラムをコンパイルおよび実行します。

  • 任意のキーを押して、コンソール ウィンドウを終了します。

参照

概念

LINQ クエリ式 (C# プログラミング ガイド)

結合演算

参照

orderby 句 (C# リファレンス)

join 句 (C# リファレンス)