将C#转换为VB.NET时出错

时间:2012-05-02 15:27:55

标签: c# vb.net c#-to-vb.net code-conversion

我正在尝试将此方法转换为VB.NET,但在线转换器似乎弄得很糟糕。有人可以提供帮助:

C#original:

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的定义......有人可以帮助我吗?

3 个答案:

答案 0 :(得分: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()进行显式调用。代码示例已更新以反映该内容。

答案 1 :(得分: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

答案 2 :(得分:0)

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