使用 LINQ to XML,如何根据序号位置连接两组数据?

发布于 2024-08-30 06:13:43 字数 471 浏览 0 评论 0原文

使用 LINQ to XML,如何根据序号位置连接两组数据?

<document>
    <set1>
        <value>A</value>
        <value>B</value>
        <value>C</value>
    </set1>
    <set2>
        <value>1</value>
        <value>2</value>
        <value>3</value>
    </set2>
</document>

基于上面的片段,我想将两个集合连接在一起,使得“A”和“1”位于同一记录中,“B”和“2”位于同一记录中,“C”和“3” " 位于同一条记录中。

Using LINQ to XML, how can I join two sets of data based on ordinal position?

<document>
    <set1>
        <value>A</value>
        <value>B</value>
        <value>C</value>
    </set1>
    <set2>
        <value>1</value>
        <value>2</value>
        <value>3</value>
    </set2>
</document>

Based on the above fragment, I would like to join the two sets together such that "A" and "1" are in the same record, "B" and "2" are in the same record, and "C" and "3" are in the same record.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烂柯人 2024-09-06 06:13:43

这就是 Enumerable.Zip 扩展的作用在 .NET 4 中。您可以像这样编写(假设这是整个 XDocument):

var set1Elements = document.Element("set1").Elements();
var set2Elements = document.Element("set2").Elements();
var results = set1Elements.Zip(set2Elements,
    (s1, s2) => new { Value1 = s1.Value, Value2 = s2.Value });

如果您使用 .NET 3.5 或更早版本,则编写 并不太难邮政编码扩展名:

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
    this IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    Func<TFirst, TSecond, TResult> resultSelector)
{
    using (var firstEnumerator = first.GetEnumerator())
    using (var secondEnumerator = second.GetEnumerator())
    {
        while ((firstEnumerator.MoveNext() && secondEnumerator.MoveNext()))
        {
            yield return resultSelector(firstEnumerator.Current,
                secondEnumerator.Current);
        }
    }
}

This is what the Enumerable.Zip extension does in .NET 4. You'd write it like so (assuming that this is the entire XDocument):

var set1Elements = document.Element("set1").Elements();
var set2Elements = document.Element("set2").Elements();
var results = set1Elements.Zip(set2Elements,
    (s1, s2) => new { Value1 = s1.Value, Value2 = s2.Value });

If you're using .NET 3.5 or earlier, it's not too difficult to write the Zip extension:

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
    this IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    Func<TFirst, TSecond, TResult> resultSelector)
{
    using (var firstEnumerator = first.GetEnumerator())
    using (var secondEnumerator = second.GetEnumerator())
    {
        while ((firstEnumerator.MoveNext() && secondEnumerator.MoveNext()))
        {
            yield return resultSelector(firstEnumerator.Current,
                secondEnumerator.Current);
        }
    }
}
岛徒 2024-09-06 06:13:43

这是使用 Select 重载的另一种方法,其中将包含元素的索引

XElement set1 = document.Root.Element("set1");
XElement set2 = document.Root.Element("set2");

var query = from value1 in set1.Descendants("value").Select((ele, idx) => new { Value = ele.Value, Index = idx })
            join value2 in set2.Descendants("value").Select((ele, idx) => new { Value = ele.Value, Index = idx })
            on value1.Index equals value2.Index
            select new { Value1 = value1.Value, Value2 = value2.Value };

Here's another method using the overload of Select that will include an element's index

XElement set1 = document.Root.Element("set1");
XElement set2 = document.Root.Element("set2");

var query = from value1 in set1.Descendants("value").Select((ele, idx) => new { Value = ele.Value, Index = idx })
            join value2 in set2.Descendants("value").Select((ele, idx) => new { Value = ele.Value, Index = idx })
            on value1.Index equals value2.Index
            select new { Value1 = value1.Value, Value2 = value2.Value };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文