我不是LINQ的常规用户所以对于专家来说这个问题可能会尽可能愚蠢:)
我在数据集中有一个表格,如下所示
Column1 OffLoc
01/02/2016 Johannesburg
01/02/2016 Johannesburg
02/02/2016 Moscow
02/02/2016 Johannesburg
02/02/2016 Johannesburg
03/02/2016 Johannesburg
03/02/2016 Moscow
03/02/2016 Moscow
03/02/2016 Bogota
04/02/2016 Barcelona
04/02/2016 Johannesburg
04/02/2016 Singapore
04/02/2016 Singapore
04/02/2016 Singapore
05/02/2016 Singapore
05/02/2016 Singapore
05/02/2016 Singapore
10/02/2016 Singapore
10/02/2016 Singapore
10/02/2016 Singapore
10/02/2016 Singapore
10/02/2016 Singapore
我想拥有以下
Column1 Offloc Count
01/02/2016 - Johannesburg - 2
02/02/2016 - Moscow - 1
02/02/2016 - Johannesburg - 2
03/02/2016 - Johannesburg - 1
03/02/2016 - Moscow - 2
03/02/2016 - Bogota - 1
...
我尝试使用此示例Linq1,但我使用数据集并避免将数据复制到列表然后执行任务。我也尝试过以下代码:
Dim oQuery = _
From oRow In ds.Table("AuditLog").AsEnumerable() _
Group By _
Column1DT = oRow("Column1"), _
OffLocDT = oRow("OffLoc") _
Into Total
但我不知道如何从这里开始。请帮我。我使用vb.net,因为在线转换器转换效果不佳。
由于
答案 0 :(得分:0)
您已经正确完成了分组。下一步,您可以将查询投影到包含所有所需信息的匿名类型,例如:
Dim oQuery = _
From oRow In ds.Table("AuditLog").AsEnumerable() _
Group By _
Column1DT = oRow("Column1"), _
OffLocDT = oRow("OffLoc") _
Into Total = Group
Select New With
{
Key .Column1 = Column1DT,
Key .OffLoc = OffLocDT,
.Count = G.Count()
}
Dim dt As New DataTable()
dt.Columns.Add("Column1", GetType(DateTime))
dt.Columns.Add("OffLoc", GetType(String))
dt.Rows.Add(New DateTime(2016, 1, 2), "Johannesburg")
dt.Rows.Add(New DateTime(2016, 1, 2), "Johannesburg")
dt.Rows.Add(New DateTime(2016, 1, 3), "Jakarta")
dt.Rows.Add(New DateTime(2016, 1, 3), "Jakarta")
dt.Rows.Add(New DateTime(2016, 1, 3), "Jakarta")
Dim result = From oRow In dt.AsEnumerable()
Group By
Column1DT = oRow("Column1"),
OffLocDT = oRow("OffLoc")
Into G = Group
Select New With
{
Key .Column1 = Column1DT,
Key .OffLoc = OffLocDT,
.Count = G.Count()
}
For Each item As Object In result
Console.WriteLine(item)
Next
输出
{ Column1 = 1/2/2016 12:00:00 AM, OffLoc = Johannesburg, Count = 2 }
{ Column1 = 1/3/2016 12:00:00 AM, OffLoc = Jakarta, Count = 3 }
答案 1 :(得分:0)
你走了。
Dim list = (From g In ds.Tables("AuditLog").AsEnumerable()
Group By
col1 = g("Column1"),
col2 = g("OffLoc")
Into X = Group
Select New With
{
Key .Column1 = col1,
Key .OffLoc = col2,
.Count = X.Count()
}).ToList()