VB.NET LINQ无法将结果转换为字典

时间:2014-07-15 20:19:01

标签: vb.net linq dictionary

我有一个列表和一个字符串列表,使用字符串中的每个字符,我需要获取包含指定字符的所有子列表,并将结果作为字典返回。

基本上我需要结果像("a": [["a","b"], ["a","c"]], "b": [["a","b"],...)一样,我试过这个:

Dim MyLists As New List(Of List(Of String)) '[["a","b"], ["a","c"], ["d","e"], ...]
Dim MyString As String = "axz"

Dim MyDict As Dictionary(Of String, List(Of List(Of String))) = (From s In MyString _
                                                                 From u In MyLists _
                                                                 Where u.Contains(s) _
                                                                 Group s Into g = Group _
                                                                 Select g).ToDictionary( _
                                                                 Function(g) g.Key, _
                                                                 Function(g) g.Value)

但是我会陷入最后一点,它应该转换为字典!有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不需要GroupBy

Dim query = From chr In MyString.Distinct()
            Where MyLists.Any(Function(l) l.Contains(chr.ToString()))
Dim dict As Dictionary(Of String, List(Of List(Of String))) = query.ToDictionary(
    Function(chr) chr.ToString(),
    Function(chr) MyLists.FindAll(Function(l) l.Contains(chr)))