vb.net LINQ-to-List 和 IEnumerable 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1449291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:25:56  来源:igfitidea点击:

LINQ-to-List and IEnumerable issues

vb.netlinqlinq-to-xmlienumerable

提问by Todd Main

I am querying an HTML file with LINQ-to-XML. It looks something like this:

我正在使用 LINQ-to-XML 查询 HTML 文件。它看起来像这样:

<html>
    <body>
        <div class="Players">
        <div class="role">Goalies</div>
        <div class="name">John Smith</div>
        <div class="name">Shawn Xie</div>
        <div class="role">Right Wings</div>
        <div class="name">Hyman Davis</div>
        <div class="name">Carl Yuns</div>
        <div class="name">Wayne Gortonia</div>
        <div class="role">Centers</div>
        <div class="name">Lutz Gaspy</div>
        <div class="name">John Jacobs</div>
        </div>
    </body>
</html>

What I'm trying to do is create a list of these folks like in a list of a structure called Players:

我想要做的是创建一个这些人的列表,就像在一个名为 Players 的结构列表中一样:

Structure Players
    Public Name As String
    Public Position As String
End Structure

But I've quickly found out I don't really know what I'm doing when it comes to LINQ.

但我很快发现,当谈到 LINQ 时,我真的不知道我在做什么。

I've got this far in my queries:

我在我的查询中已经走了这么远:

Dim goalieList = From d In player.Elements _
                 Where d.Value = "Goalies" _
                 Select From g In d.ElementsAfterSelf _
                 Take While (g.@class <> "role") _
                 Select New Players With {.Position = "Goalie", _
                         .Name = g.Value}

Dim centersList = From d In player.Elements _
                  Where d.Value = "Centers" _
                  Select From g In d.ElementsAfterSelf _
                  Take While (g.@class <> "role") _
                  Select New Players With {.Position = "Centers", _
                         .Name = g.Value}

Which gets me down to the players by position, but then I can't do much with this afterwards the result type is System.Collections.Generic.IEnumerable(Of System.Collections.Generic.IEnumerable(Of Player))

这让我按位置找到了球员,但是之后我无法做太多事情,结果类型是 System.Collections.Generic.IEnumerable(Of System.Collections.Generic.IEnumerable(Of Player))

What I want to do is add these two results to a new list, like:

我想要做的是将这两个结果添加到一个新列表中,例如:

Dim playersList As List(Of Players) = Nothing
playersList.AddRange(centersList)
playersList.AddRange(goalieList)

So that I can then query the list and use it. But it kicks the error:

这样我就可以查询列表并使用它。但它会引发错误:

Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Xml.Linq.XElement,System.Collections.Generic.IEnumerable1[Players]]' to type 'System.Collections.Generic.IEnumerable`1[Players]'

无法将“WhereSelectEnumerableIterator 2[System.Xml.Linq.XElement,System.Collections.Generic.IEnumerable1[Players]]”类型的对象转换为“System.Collections.Generic.IEnumerable`1[Players]”

As you can see, I may really have no idea how to work with all these objects/classes. Does anyone have any insight on what I may be doing wrong and how I can resolve it?

如您所见,我可能真的不知道如何处理所有这些对象/类。有没有人对我可能做错了什么以及我如何解决它有任何见解?

RESOLVED: The LINQ query needs to return a single IEnumerable, like this:

已解决:LINQ 查询需要返回单个 IEnumerable,如下所示:

Dim goalieList = From l In _
                    (From d In players.Elements _
                     Where d.Value = "Goalies" _
                     Select d.ElementsAfterSelf.TakeWhile(Function(f) f.@class <> "role")) _
                  Select New Players With {.Position = "Goalie", .Name = l.Value}

and then use goalieList.ToList

然后使用 goalieList.ToList

采纳答案by Alan

I fail at VB, but in C# you would call ToList() on the centersList and goalieList.

我在 VB 上失败了,但是在 C# 中,您会在中心列表和守门员列表上调用 ToList()。

So something like:

所以像:

playersList.AddRange(centersList.ToList())
playersList.AddRange(goalieList.ToList())

回答by Igor ostrovsky

Any chance that you have two Players classes? Otherwise, I don't see how you could possibly get that error message.

您是否有机会拥有两个 Players 课程?否则,我看不到您怎么可能收到该错误消息。

Looking into Reflector, WhereSelectEnumerableIterator implements IEnumerable, so the cast should succeed.

查看 Reflector,WhereSelectEnumerableIterator 实现了 IEnumerable,因此转换应该会成功。