c# - 将 LINQ 表达式从 C# 转换为 VB.NET 在 Group by 中引发异常

标签 c# vb.net linq

我在 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/

相关文章:

c# - 如何在ABP框架中复制SQL的ORDER BY NEWID()?

c# - 检查复选框值并将其分配给 C# 中的字符串

vb.net - 如何绘制 (x,y,z)

c# - 为什么第一个 linq 的执行速度比第二个快 20 倍 [获取最大值]

mysql - vb.net 消费nusoap报错

xml - 如何解析多个不唯一的 XML 元素

c# - FO-DICOM:在 C# Windows 窗体应用程序中使用呈现的位图调整窗口大小会导致崩溃

c# - 是否可以编写使用 xamarin 表单(UI)和 Unity(3D 部分)开发的非游戏跨平台 3D 可视化移动应用程序?

c# - 什么情况下 TryDequeue 和类似的 System.Collections.Concurrent 收集方法会失败

c# - 如何使用 LINQ 检索 SharePoint 用户数据?