IEnumerable 将 2 个 IEnumerable 转换为一个类

发布于 2024-10-07 12:27:05 字数 1308 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-10-14 12:27:05

您可以借助 Enumerable.Zip 方法,前提是您的 XML 是平衡的(偶数个元素来配对距离和点)。

var query = xml.Elements("Distance")
               .Zip(xml.Elements("Point"),
                    (d, p) => new Subsection
                    {
                        Distance = float.Parse(d.Value),
                        Point = p
                    });

或者,您可以循环遍历元素并构建 Subsection 项。这可以按如下方式完成,尽管它假设您的 XML 文档是平衡的并且采用预期的格式。

var query = xml.Elements()
               .Where(e => e.Name.LocalName == "Distance"
                           || e.Name.LocalName == "Point");
var list = new List<Subsection>();
int count = 0;
Subsection s = null;
foreach (var element in query)
{
    if (count % 2 == 0)
        s = new Subsection { Distance = float.Parse(element.Value) };
    else
    {
        s.Point = element;
        list.Add(s);
    }

    count++;
}

请注意,在这两个代码段中,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).

var query = xml.Elements("Distance")
               .Zip(xml.Elements("Point"),
                    (d, p) => new Subsection
                    {
                        Distance = float.Parse(d.Value),
                        Point = p
                    });

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.

var query = xml.Elements()
               .Where(e => e.Name.LocalName == "Distance"
                           || e.Name.LocalName == "Point");
var list = new List<Subsection>();
int count = 0;
Subsection s = null;
foreach (var element in query)
{
    if (count % 2 == 0)
        s = new Subsection { Distance = float.Parse(element.Value) };
    else
    {
        s.Point = element;
        list.Add(s);
    }

    count++;
}

Note that in both snippets the xml variable is an XElement. For an XDocument add the Root property, as in xml.Root.Elements(...).

你好,陌生人 2024-10-14 12:27:05

这不是引用透明的,也是完全不可取的,但如果您热衷于 LINQ,那么这是我目前针对这个特定问题的最佳想法。这样您的订单就可以保证是相同的。

public class SubSection
{
    public float Distance;
    public XElement Point;

    public SubSection(XElement distance, XElement point)
    {
        Distance = float.Parse(distance.Value);
        Point = point;
    }
}
class Program
{
    static void Main(string[] args)
    {
        var c = XDocument.Parse(@"<PLI>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>");

        var sup = new List<SubSection>();
        c.Elements().Elements().Aggregate<XElement,XElement>(null, (a, d) =>
            {
                if (a == null)
                    return d;
                sup.Add(new SubSection(a, d));
                return null;
            });
        foreach (var entry in sup)
        {
            Console.WriteLine(entry.Distance);
        }

    }
}

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.

public class SubSection
{
    public float Distance;
    public XElement Point;

    public SubSection(XElement distance, XElement point)
    {
        Distance = float.Parse(distance.Value);
        Point = point;
    }
}
class Program
{
    static void Main(string[] args)
    {
        var c = XDocument.Parse(@"<PLI>
<Distance>5</Distance>
<Point>23 22</Point>
<Distance>7</Distance>
<Point>21 72</Point>
<Distance>9</Distance>
<Point>13 32</Point>
</PLI>");

        var sup = new List<SubSection>();
        c.Elements().Elements().Aggregate<XElement,XElement>(null, (a, d) =>
            {
                if (a == null)
                    return d;
                sup.Add(new SubSection(a, d));
                return null;
            });
        foreach (var entry in sup)
        {
            Console.WriteLine(entry.Distance);
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文