0

我正在尝试将此方法转换为 VB.NET,但在线转换器似乎将其转换为一团糟。有人可以帮忙吗:

C# 原文:

private IEnumerable<IGrouping<string, Reference>> FindReferencesWithTheSameShortNameButDiffererntFullNames(List<Reference> references)
        {
            return from reference in references
                   group reference by reference.ReferencedAssembly.Name
                       into referenceGroup
                       where referenceGroup.ToList().Select(reference => reference.ReferencedAssembly.FullName).Distinct().Count() > 1
                       select referenceGroup;
        }

VB.NET 使用在线转换器:

Private Function FindReferencesWithTheSameShortNameButDiffererntFullNames(references As List(Of Reference)) As IEnumerable(Of IGrouping(Of String, Reference))
        Return _
            Where referenceGroup.ToList().[Select](Function(reference) reference.ReferencedAssembly.FullName).Distinct().Count() > 1
End Function

Where未声明此错误。

我不会想到 VB 不会像这样的 C# 那样不同:

Private Function FindReferencesWithTheSameShortNameButDiffererntFullNames(references As List(Of Reference)) As IEnumerable(Of IGrouping(Of String, Reference))
            Return From reference In references
                   Group reference By reference.ReferencedAssembly.Name
                   Into referenceGroup()
                   Where referenceGroup.ToList().Select(Function(reference) ReferencedAssembly.FullName).distinct().count() > 1
                                                        Select referenceGroup

        End Function

但我得到:在这种情况下无法访问方法 referenceGroup 的定义......有人可以帮助我吗?

4

3 回答 3

2

VB.Net 的语法略有不同。该Group关键字还指示当前分组,可以直接使用或分配给命名变量。

 Private Function FindReferencesWithTheSameShortNameButDiffererntFullNames(references As List(Of Reference)) As IEnumerable(Of IGrouping(Of String, Reference))
    Return From referenceGroup In references.GroupBy(Of String, Reference)(Function(reference) reference.ReferencedAssembly.Name, Function(reference) reference)
           Where (referenceGroup.ToList().Select(Function(reference) As String
                                                     Return reference.ReferencedAssembly.FullName
                                                 End Function).Distinct().Count() > 1)
           Select referenceGroup
End Function

更新

我刚刚注意到你正在返回一个IGrouping. 查询理解语法在这种情况下不起作用,您必须显式调用GroupBy(). 代码示例已更新以反映这一点。

于 2012-05-02T16:57:14.803 回答
2

原始 C# 代码做了一些不必要的操作,所以让我们先清理它并做一些优化。

调用是不必要的ToList(),删除它。调用Count()枚举所有项目以获取计数,但您只想检查是否有多个。最好跳过第一个结果,看看是否还有其他结果,并且在逻辑上是等价的。

private IEnumerable<IGrouping<string, Reference>>
    FindReferencesWithTheSameShortNameButDiffererntFullNames(
        List<Reference> references)
{
    return
        from reference in references
        group reference by reference.ReferencedAssembly.Name into referenceGroup
        where referenceGroup
            .Select(reference => reference.ReferencedAssembly.FullName)
            .Distinct()
            .Skip(1)
            .Any()
        select referenceGroup;
}

所以翻译这个:

Private Function FindReferencesWithTheSameShortNameButDiffererntFullNames(references As List(Of Reference)) As IEnumerable(Of IGrouping(Of String, Reference))
    Return
        From referenceGroup In references.GroupBy(Function(reference) reference.ReferencedAssembly.Name)
        Where referenceGroup.Select(Function(reference) reference.ReferencedAssembly.FullName).Distinct.Skip(1).Any
        Select referenceGroup
End Function
于 2012-05-02T17:29:58.563 回答
0

是否像从 referenceGroup 中删除括号一样简单?我想知道 vb 是否认为这是一种方法而不是标识符。

于 2012-05-02T17:00:04.543 回答