【问题标题】:Translation of c# linq to vb - overload resolution failure in intellisense with 'selectmany' statementc# linq 到 vb 的翻译 - 使用“selectmany”语句在智能感知中重载解析失败
【发布时间】:2012-11-12 10:39:19
【问题描述】:

我已经尝试了所有的翻译服务来获得正确的语法,但我仍然得到“重载解析失败,因为无法使用这些参数调用任何可访问的 'SelectMany'” 在 select 语句的第一部分(直到 groupby 关键字之前的句号)

我试图在本地工作的在线示例中的原始 c# 语句:

public IEnumerable<TagGroup> GetTagGroups()
{

    var tagGroups = 
        // extract the delimited tags string and session id from all sessions
        DbSet.Select(s => new { s.Tags, s.Id })
            .ToArray() // we'll process them in memory.

            // split the "Tags" string into individual tags 
            // and flatten into {tag, id} pairs
            .SelectMany(
                s =>
                s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries)
                    .Select(t => new { Tag = t, s.Id })
            )

            // group {tag, id} by tag into unique {tag, [session-id-array]}
            .GroupBy(g => g.Tag, data => data.Id)

            // project the group into TagGroup instances
            // ensuring that ids array in each array are unique
            .Select(tg => new TagGroup 
                            {
                                Tag = tg.Key, 
                                Ids = tg.Distinct().ToArray(),
                            })
            .OrderBy(tg => tg.Tag);

    return tagGroups;
}

我在 VB 中最接近它:

Public Function GetTagGroups() As IEnumerable(Of TagGroup)

    ' extract the delimited tags string and session id from all sessions
    ' we'll process them in memory.
    ' split the "Tags" string into individual tags 
    ' and flatten into {tag, id} pairs

    ' group {tag, id} by tag into unique {tag, [session-id-array]}

    ' project the group into TagGroup instances
    ' ensuring that ids array in each array are unique

    Dim tagGroups = DbSet.[Select](Function(s) New With { _
        s.Tags, _
        s.Id _
    }).ToArray().SelectMany(Function(s) s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) New With { _
        Key .Tag = t, _
        s.Id _
    })).GroupBy(Function(g) g.Tag, Function(data) data.Id).[Select](Function(tg) New With { _
        Key .Tag = tg.Key, _
        Key .Ids = tg.Distinct().ToArray() _
    }).OrderBy(Function(tg) tg.Tag)

    Return tagGroups
End Function

这会导致 Visual Studio 2012 intellisense 将语句的第一部分从第一行的“DbSet”到靠近底部的“.GroupBy”之前的最后一个括号用蓝色下划线。错误是“重载解析失败,因为无法使用这些参数调用可访问的 'SelectMany'”。

由于这是一个代码示例,我正在尝试转换为 vb 以在本地运行并理解,而我对 linq 的经验不足,我完全不知道如何尝试和处理这个问题。这超出了我目前的理解范围,因此可能是一个简单的语法错误或从头到尾的完整哈希!

非常感谢任何指点。

【问题讨论】:

    标签: vb.net entity-framework linq


    【解决方案1】:

    我现在已经把它放在了 VS2k8 中,以及我之前提到的 VB 示例的两个简单问题:

    • 前两个New With 构造必须指定.&lt;field&gt; = AFAIK,并且
    • 最后一个New With 不应该是匿名的。

    我现在还注意到这些是我需要让代码没有错误的声明。除了添加中间查询变量(顺便说一句,这意味着您可以将 C# cmets 重新插入),我不记得实际上进一步更改了代码。注意_tagDelimiterChar()——C# 代码将其声明为什么? (查看提到StringSplitOptionsString.Split 重载,它必须是Char()String() 或C# 在VB.NET 没有的地方隐式更改类型。)

    Class TagList
        Public Tags As String
        Public Id As String
    End Class
    
    Private DbSet As IQueryable(Of TagList)
    
    Class TagGroup
        Public Tag As String
        Public Ids() As String
    End Class
    
    Private _tagDelimiter As Char()
    
    Public Function GetTagGroups() As IEnumerable(Of TagGroup)
    
        Dim theTags = DbSet.[Select](Function(s) New With { _
            .Tags = s.Tags, _
            .Id = s.Id _
        }).ToArray()
    
        Dim many = theTags.SelectMany(Function(s) s.Tags.Split(_tagDelimiter, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) New With { _
            Key .Tag = t, _
            .Id = s.Id _
        }))
    
        Dim grouped = many.GroupBy(Function(g) g.Tag, Function(data) data.Id)
    
        Dim unordered = grouped.[Select](Function(tg) New TagGroup With { _
            .Tag = tg.Key, _
            .Ids = tg.Distinct().ToArray() _
        })
        Dim tagGroups = unordered.OrderBy(Function(tg) tg.Tag)
    
        Return tagGroups
    End Function
    

    【讨论】:

    • 谢谢,但没有结果......在 IDE 中也是如此,“重载解析失败,因为无法使用这些参数调用可访问的 'SelectMany'”。像以前一样,从第一行的第一个 DbSet 一直到“.GroupBy”之前的最后一个括号都加下划线。
    • 谢谢,谢谢!只是其中有错误的 tagdelimiter 声明。它是: Private ReadOnly _tagDelimiter As Char() = {"|"c}
    猜你喜欢
    • 2020-03-10
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多