いっつも忘れるので、めも。
Module Module2
Sub Main()
Dim samples As New List(Of KeyData)
Dim rnd As New Random
Dim KeysCount = rnd.Next(1, 120)
Console.WriteLine("{0} 件キー", KeysCount)
For x As Integer = 1 To KeysCount
samples.Add(New KeyData With {
.Key1 = rnd.Next(1, 3).ToString("KeyA-000"),
.Key2 = rnd.Next(1, 3).ToString("KeyB-000"),
.Key3 = rnd.Next(1, 3).ToString("KeyC-000"),
.Data1 = rnd.Next(100, 500),
.Data2 = Now.AddDays(rnd.Next(-100, 100))
})
Next
Console.WriteLine("件数 : {0}", samples.Count)
Dim wlist = (From x In samples
Group By x.Key1, x.Key2, x.Key3
Into Group
Select Group).Select(Function(f) f.OrderBy(Function(r) r.Data2).FirstOrDefault).ToList
Console.WriteLine("グルーピング件数 : {0}", wlist.Count)
wlist.ForEach(Sub(x)
Console.WriteLine("キーごとで最も過去日付のレコード : {0}", x.ToString)
End Sub)
Console.ReadKey()
End Sub
End Module
Public Class KeyData
Property Key1 As String
Property Key2 As String
Property Key3 As String
Property Data1 As Integer
Property Data2 As Date
Public Overrides Function ToString() As String
Return String.Format("{0}.{1}.{2}({3}:{4})", Key1, Key2, Key3, Data1, Data2.ToString)
End Function
End Class
その結果