IEnumerable 将 2 个 IEnumerable 转换为一个类
我有 2 个 IEnumerables
IEnumerable<float> Distance
IEnumerable<XElement> Point
,我想将其转换为
IEnumerable<Subsection> subsection
类所在的位置
class Subsection
{
public float Distance
public XElement Point
}
,但我不知道如何执行此操作,我尝试了一些强制转换的变体,但没有一个起作用,因为它们似乎不接受多个列表作为输入。
Distance 和 Point 变量是从 xml 文档中读取的,其中这两个点的结构类似于:
<PLI>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>
我不确定如何将它们简单地作为小节类型读出,但如果有人可以建议如何做到这一点,它将绕过我需要将其转换,因为我将不再将它们作为距离和点的 IEnumerables 而是作为结构。
请注意,我无法修改 XML,
谢谢
编辑: XML 还包含其他元素以及 PLI 标签中提到的元素,例如
<PLI>
<OtherElement1>element1value</OtherElement1>
<OtherElement2>element2value</OtherElement2>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>
I have 2 IEnumerables
IEnumerable<float> Distance
IEnumerable<XElement> Point
which i want to convert into
IEnumerable<Subsection> subsection
where the Class is
class Subsection
{
public float Distance
public XElement Point
}
But i have no idea how to do this, i have tried some variations of casting none of which has worked because they dont seem to accept multiple lists as inputs.
The Distance and Point variables are read from a xml document where the structure for these two points is similar to:
<PLI>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>
I wasn't sure how to read them out simply as the subsection type but if anyone could suggest how to do that, it would bypass my need to convert it as i will no longer have them as IEnumerables of distance and point but as the structure.
Please note I cannot modify the XML
Thanks
EDIT: The XML has other elements as well as the ones mentioned within the PLI Tag e.g.
<PLI>
<OtherElement1>element1value</OtherElement1>
<OtherElement2>element2value</OtherElement2>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以借助 Enumerable.Zip 方法,前提是您的 XML 是平衡的(偶数个元素来配对距离和点)。
或者,您可以循环遍历元素并构建
Subsection
项。这可以按如下方式完成,尽管它假设您的 XML 文档是平衡的并且采用预期的格式。请注意,在这两个代码段中,
xml
变量都是XElement
。对于XDocument
添加Root
属性,如xml.Root.Elements(...)
中所示。You can pull this off with LINQ with the help of the Enumerable.Zip method, provided your XML is balanced (an even number of elements to pair up distances and points).
Alternately, you could loop through the elements and build up the
Subsection
items. This can be done as follows, although it assumes your XML document is balanced and in the expected format.Note that in both snippets the
xml
variable is anXElement
. For anXDocument
add theRoot
property, as inxml.Root.Elements(...)
.这不是引用透明的,也是完全不可取的,但如果您热衷于 LINQ,那么这是我目前针对这个特定问题的最佳想法。这样您的订单就可以保证是相同的。
This is not referentially transparent and completely inadvisable, but if you have your heart set on LINQ, this is the best idea I have for now for this particular problem. This way your order is guaranteed to be the same.