複合キーを匿名データ型で指定する以外は通常のグループ集計処理と同じ。
この様な処理ではあまりLINQのメリットは感じられない。(SQLで書いたほうが分かりやすく簡単かも)
- //複合キーでのグループ化処理
- public void linqGroupComplex(Form1 form)
- {
- using (NorthWindDataContext nwnd = new NorthWindDataContext())
- {
- nwnd.Log = Console.Out;
- var customers = nwnd.Customers.GroupBy(c => new {c.Country,c.City})
- .OrderBy(g => g.Key.Country)
- .ThenBy(g => g.Key.City)
- .Select(g => new
- {
- g.Key.Country,
- g.Key.City,
- count = g.Count()
- });
- form.dataGridView1.DataSource = customers;
- }
- }
埋め込みクエリー方式では以下のとおり。
- //複合キーでのグループ化処理
- public void linqGroupComplex(Form1 form)
- {
- using (NorthWindDataContext nwnd = new NorthWindDataContext())
- {
- var customers = from c in nwnd.Customers
- group c by new{c.Country,c.City} into g
- orderby g.Key.Country,g.Key.City
- select new
- {
- g.Key.Country,
- g.Key.City,
- count = g.Count()
- };
- form.dataGridView1.DataSource = customers;
- Console.Beep();
- }
- }
0 件のコメント:
コメントを投稿