使用LINQ到XML,如何基于序数位置连接两组数据?[英] Using LINQ to XML, how can I join two sets of data based on ordinal position?

本文是小编为大家收集整理的关于使用LINQ到XML,如何基于序数位置连接两组数据?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用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"在同一记录中.

推荐答案

这就是 扩展名在.NET 4中.

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或更早,那么编写Zip扩展名并不难:

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);
        }
    }
}

其他推荐答案

这是另一种使用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 };

本文地址:https://www.itbaoku.cn/post/1557112.html

问题描述

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.

推荐答案

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);
        }
    }
}

其他推荐答案

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 };