将 LINQ 从 C# 转换为 VB.NET 时出现问题

Problem translating LINQ from C# to VB.NET

我有一个在 C# 中按预期工作的 LINQ 查询。我需要将其翻译成 VB.NET.

None 的在线翻译人员给出了一个可行的版本

C#(工作中)

var result = from r1 in rows
                         join r2 in rows
                            on new { r1.MeterIdentifier, r1.Timestamp } equals
                                new { r2.MeterIdentifier, r2.Timestamp }
                         group r1.billNo by r1.billNo into g
                         select new
                         {
                             BillNo = g.Key,
                             Count = g.Count()
                         };

VB.NET(从上面翻译)不工作:它 returns 空的:

Dim result = From r1 In rows Join r2 In rows On
                             New With {r1.MeterIdentifier, r1.Timestamp} Equals
                             New With {r2.MeterIdentifier, r2.Timestamp}
                                              Group r1.BillNo By __groupByKey1__ = r1.BillNo Into g = Group
                                              Select New With
                                                    {
                                                        .BillNo = __groupByKey1__,
                                                        .Count = g.Count()
                                                    }

            Dim test = result.ToList() ' list empty

Telerik 的翻译器给出了一个非常不同的代码,但它甚至没有编译。

我对 LINQ 非常有限,任何人都可以发现翻译是否 wrong/how 来修复它?

非常感谢

来自后台

think of it as import code should only cover one distinct fromDate. If it appears multiple time, I would like to see how many times (count) and which BillNo s are like that

我想我只是按 ImportCode 分组并得到那些 Min fromDate 不等于 Max fromDate 的那些..它也会给出账单号码

data _
  .GroupBy(Function(x) x.ImportCode) _
  .Where(Function(g) g.Select(Function(gg) gg.FromDate).Min() <> g.Select(Function(gg) gg.FromDate).Max()) 

或者使用多行 lambda

data _
  .GroupBy(Function(x) x.ImportCode) _
  .Where(Function(g) _
    Dim h = g.Select(Function(gg) gg.FromDate) _
    Return h.Min() <> h.Max() _
  End Function) 

你得到的是一堆分组,这实际上是原始对象列表 B 的列表 A。

有道理吗?没有..好的

假设这是 MeterReading(?) 对象的单个列表,例如(我省略了 BillNo...):

ImportCode  Date
A           1-Jan
A           1-Jan
A           2-Jan
B           1-Jan
B           2-Jan
B           3-Jan
C           1-Jan

按ImportCode分组后,变成了类似(JSON风味渲染)的仪表读数列表:

Key            (items list - a bunch of MeterReading)
A              [{A,1-Jan},{A,1-Jan},{A,2-Jan}]
B              [{B,1-Jan},{B,2-Jan},{B,3-Jan}]
C              [{C,1-Jan}]

如果你说 Where(Function(items) ...) 那么 Where 中的 ... 中的代码是 运行 上面每个分组行一次 - 有 3 行所以代码被调用了 3 次。一行是一个 items - 它有一个 .Key 属性 (我不使用)这是分组的项目代码,整个 items 东西本身是进入分组操作的输入MeterReading对象的列表(准确地说是可枚举的),所以换句话说,items是所有具有相同ImportCode的MeterReading列表,并且有3个这样的列出

如果您 Select items 中每个 MeterReading 的 FromDate 并调用 Max 那么您将获得最大日期,最小日期相同。如果它们不相等,那么 items 列表包含一些具有不同日期的仪表读数 - 你说它们应该相同。对于以项目代码 A 和 B 为首的列表,最小日期与最大日期不同。对于 C,它们都是一样的,所以 C 从结果中消失了,我们减少到 2 行

Key            (items list - a bunch of MeterReading)
A              [{A,1-Jan},{A,1-Jan},{A,2-Jan}]
B              [{B,1-Jan},{B,2-Jan},{B,3-Jan}]

Where 的输出是“两行”,每行都有所有仪表读数及其相关数据的完整列表。您可以通过多种方式提取该数据,例如:

.Select(Function(meterReadingsList) _
  New With { _
    .DistinctDayCount = meterReadingsList.Select(Function(mr) mr.FromDate).Distinct().Count() _ 'the number of different days this happened on
    .BillNos = meterReadingsList.Select(Function(mr) mr.BillNo).ToArray() _
 } _
End Function)

在 linq 中还有一个 SelectMany 操作,可以用来将一个 List Of List Of Something 变成一个 List Of Something(else);有点像之前完成的 GroupBy 的反向操作。如果您要 SelectMany 这两行中的仪表读数,它们会像我们开始时那样返回到一个列表中..

ImportCode  Date
A           1-Jan
A           1-Jan
A           2-Jan
B           1-Jan
B           2-Jan
B           3-Jan

但是从你所说的想要对每个项目代码进行计数等看来,他们最好还是使用 2D 表示