【LINQ】 VB.NET で GroupBy する。

VB.NET ソースの改修で、どはまりしたのでメモ。
複数項目でグループ化する際に匿名型を使うときは「Key」と記述しないとグループ化されない。

```c#
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest {
	[TestClass()]
	public class LinqTests {
		[TestMethod()]
		public void GroupByTest() {
			var items = new[] {
				new { 産地 = "埼玉", 種類 = "果物", 名前 = "リンゴ", 価格 = 100, },
				new { 産地 = "埼玉", 種類 = "果物", 名前 = "オレンジ", 価格 = 120, },
				new { 産地 = "埼玉", 種類 = "野菜", 名前 = "キャベツ", 価格 = 90, },
				new { 産地 = "埼玉", 種類 = "野菜", 名前 = "レタス", 価格 = 110, },
				new { 産地 = "埼玉", 種類 = "穀物", 名前 = "ピーナッツ", 価格 = 90, },
				new { 産地 = "埼玉", 種類 = "穀物", 名前 = "とうもろこし", 価格 = 110, },
				new { 産地 = "千葉", 種類 = "果物", 名前 = "リンゴ", 価格 = 110, },
				new { 産地 = "千葉", 種類 = "果物", 名前 = "オレンジ", 価格 = 110, },
				new { 産地 = "千葉", 種類 = "野菜", 名前 = "キャベツ", 価格 = 100, },
				new { 産地 = "千葉", 種類 = "野菜", 名前 = "レタス", 価格 = 100, },
				new { 産地 = "千葉", 種類 = "穀物", 名前 = "ピーナッツ", 価格 = 80, },
				new { 産地 = "千葉", 種類 = "穀物", 名前 = "とうもろこし", 価格 = 120, },
			};

			var query = (
				from item in items
				group item by new {
					item.産地,
					item.種類,
				}
			);

			var ls = query.ToList();

			Assert.AreEqual(6, ls.Count);
		}
	}
}
```

```vb
Imports System.Linq
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Namespace UnitTest
    <TestClass()>
    Public Class LinqTests
        <TestMethod()>
        Public Sub GroupByTest()
            Dim items = {
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "リンゴ", .価格 = 100},
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "オレンジ", .価格 = 120},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "レタス", .価格 = 110},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "リンゴ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "オレンジ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "レタス", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 80},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 120}
            }

            Dim query = (
                From item In items
                Group By
                    item.産地,
                    item.種類
                Into Group
            )

            Dim ls = query.ToList()
            Assert.AreEqual(6, ls.Count)
        End Sub

        <TestMethod()>
        Public Sub GroupByTest2()
            Dim items = {
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "リンゴ", .価格 = 100},
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "オレンジ", .価格 = 120},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "レタス", .価格 = 110},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "リンゴ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "オレンジ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "レタス", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 80},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 120}
            }

            Dim query = items.GroupBy(Function(item) New With {
                Key item.産地,
                Key item.種類
            })

            Dim ls = query.ToList()
            Assert.AreEqual(6, ls.Count)
        End Sub

        <TestMethod()>
        Public Sub GroupByTest3()
            Dim items = {
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "リンゴ", .価格 = 100},
                New With {.産地 = "埼玉", .種類 = "果物", .名前 = "オレンジ", .価格 = 120},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "野菜", .名前 = "レタス", .価格 = 110},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 90},
                New With {.産地 = "埼玉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "リンゴ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "果物", .名前 = "オレンジ", .価格 = 110},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "キャベツ", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "野菜", .名前 = "レタス", .価格 = 100},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "ピーナッツ", .価格 = 80},
                New With {.産地 = "千葉", .種類 = "穀物", .名前 = "とうもろこし", .価格 = 120}
            }

            Dim query = items.GroupBy(Function(item) New With {
                item.産地,
                item.種類
            })

            Dim ls = query.ToList()
            Assert.AreEqual(12, ls.Count)
        End Sub
    End Class
End Namespace
```

# 参考サイト
* [.NET（VB）LINQをVBで使用する際に注意すること](http://yan-note.blogspot.com/2015/11/netvblinqvb.html?spref=bl)
* [VB.NETでもLINQでGroupByしたい](https://jyuch.hatenablog.com/entry/2015/02/06/222306)
* [【C#入門】LINQのGroupByでグループ化する(OrderByも解説)](https://www.sejuku.net/blog/47220)