我在 C# 中有以下 Linq 表达式,它运行良好:
from c3 in equipment.Measures
where c3.IdEq == eq.IdEq && !(c3.magnitudeId == null || c3.magnitudeId.Trim() == string.Empty)
group c3 by c3.magnitudeId into cgroup
select new
{
MagnitudeID = cgroup.Key,
MaxDate = cgroup.Max(x => x.NextDate)
}
现在我尝试将其转换为 VB.NET 表示法:
From c3 In equipment.Measures
Where c3.IdEq = eq.IdEq AndAlso Not (String.IsNullOrWhiteSpace(c3.magnitudeId))
Group c3 By c3.magnitudeId Into grp = Group
Select New With
{
.MagnitudeID = grp.Key,
.MaxDate = grp.Max(Function(x) x.NextDate)
}
...但我收到一条错误消息:
'key' is not a member of 'System.Collection.Generics.IEnumberable(Of ...)'
最佳答案
尝试以下操作(另请注意,为了等效,您需要在 VB 中使用“Key”关键字):
From c3 In equipment.Measures
Where c3.IdEq = eq.IdEq AndAlso Not (c3.magnitudeId Is Nothing OrElse c3.magnitudeId.Trim() = String.Empty)
Group c3 By c3.magnitudeId Into cgroup = Group
Select New With {
Key .MagnitudeID = magnitudeId,
Key .MaxDate = cgroup.Max(Function(x) x.NextDate)
}
如果 C# 和 VB 团队能够稍微协调一下就好了,因为 C# 和 VB 之间 LINQ 的一些差异非常令人费解。
关于c# - 将 LINQ 表达式从 C# 转换为 VB.NET 在 Group by 中引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59394328/