我在数据表上使用 VB 中的 LINQ 来过滤我的结果。
m_oDataTable.Columns.Add("Name1", GetType(String))
m_oDataTable.Columns.Add("Name2", GetType(String))
m_oDataTable.Columns.Add("Time", GetType(Double))
我正在尝试通过Name1
或Name2
基于用户选择来过滤数据表。
groupBy = "Name1"
'groupBy = "Name2"
我对数据进行了分组,但无法找到Sum
所需的字段。
Dim test = From tab In m_oDataTable.AsEnumerable()
Group tab By groupDt = tab.Field(Of String)(groupBy) Into Group
Select Group
'Getting Error
Dim test2 = From tab In m_oDataTable.AsEnumerable()
Group tab By groupDt = tab.Field(Of String)(groupBy) Into Group
Select New With
{
.Grp = Key, ' Error in this line(Key is a type, cannot be used as a expression)
.Sum = Group.Sum(Function(r) Double.Parse(r.Item("Time").ToString()))
}
我在 c# 中尝试过并得到了想要的结果。但是对VB没有运气:(
var test = from tab in m_oDataTable.AsEnumerable()
group tab by tab.Field<string>(groupBy)
into groupDt
select new
{
Group = groupDt.Key,
Sum = groupDt.Sum(r => double.Parse(r["Time"].ToString()))
};
如何在VB中实现这一点?