3

太陽の下ですべての翻訳サービスを試してこの構文を正しくしましたが、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

これにより、ビジュアル スタジオ 2012 の IntelliSense では、ステートメントの最初の部分の最初の行にある "DbSet" から、一番下にある ".GroupBy" の前の最後の括弧までに青色の下線が引かれます。エラーは、「アクセス可能な 'SelectMany' をこれらの引数で呼び出すことができないため、オーバーロードの解決に失敗しました」です。

これはコード例であるため、ローカルで実行して理解するためにvbに変換しようとしていますが、linqの経験が十分ではないため、これを試して対処する方法が完全に失われています。これは私の現在の理解をはるかに超えているため、単純な構文エラーまたは最初から最後までの完全なハッシュである可能性があります。

任意のポインタに非常に感謝します。

4

1 に答える 1

3

これを VS2k8 に入れました。また、以前に指摘した VB サンプルの 2 つの単純な問題も同様です。

  • 最初の 2 つの構成要素はAFAIK を New With指定する必要があり、.<field> =
  • 最後New Withは匿名であってはなりません。

また、これらは、コードにエラーがないようにするために必要な宣言であることに注意してください。中間クエリ変数を追加する以外に (これは、C# コメントを元に戻すことができることを意味します)、実際にコードをさらに変更したことを思い出しません。_tagDelimiteras a に注意してくださいChar()-- C# コードはそれを何として宣言しましたか? (またはまたは C#でなければならないことを示すString.Splitオーバーロードを見ると、 VB.NET が変更しないどこかで型を暗黙的に変更しています。)StringSplitOptionsChar()String()

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
于 2012-11-15T08:51:41.933 に答える