开发者

How to merge data in columns in linq

开发者 https://www.devze.com 2023-02-08 16:20 出处:网络
I have a source data in xml format like; <plans> <plan> <id>1</id> <name>Test1</name>

I have a source data in xml format like;

    <plans>
    <plan>
        <id>1</id>
        <name>Test1</name>
        <description>Description for test 1</description>
    </plan>
    <plan>
        <id>2</id>
        <name>Test2</name>
        <description>Description for test 2</description>
    </plan>
    <plan>
        <id>3</id>
        <name>Test3</name>
        <description>Description for test 3</description>
    </plan>
   </plans>

And I have details data which is also in xml format like;

    <plandetails>
    <detail>
        <planid>1</planid>
        <properties>
            <propery>
                <name>Pname1</name>
                <value>Test1Property1</value>
                <name>Pname2</name>
                <value>Test1Property2</value>
                <name>Pname3</name>
                <value>Test1Property3</value>
            </propery>
        </properties>
    </detail>
    <detail>
        <planid>2</planid>
        <properties>
            <propery>
                <name>Pname1</name>
                <value>Test2Property1</value>
                <name>Pname2</name>
                <value>Test2Property2</value>
                <name>Pname3</name>
                <value>Test2Property3</value>
            </propery>
        </properties>
    </detail>
    <detail>
        <planid>3</planid>
        <properties>
            <propery>
                <name>Pname1</name>
                <value>Test3Property1</value>
                <name>Pname2</name>
                <value>Test3Property2</value>
                <name>Pname3</name>
                <value>Test3Property3</value>
            </propery>
        </properties>
    </detail>   
</plandetails>

I am trying to get result like;

Name           Test1                   Test2                    Test3                   ....  Test n
Description Description for test 1  Description for test 2   Description for test 3  ....  Description for test n
Pname1       Test1Property1          Test2Property1           Test3Property1          ....  TestnProperty1
Pname2       Test1Property2          Test2Property2           Test3Property2          ....  TestnProperty2
Pname3       Test1Property3        开发者_StackOverflow  Test2Property3           Test3Property3          ....  TestnProperty3
.
.
.
.

How can I achieve this with LINQ?

Thanks in advance.


Depends on the language. In C# this looks like a job for Zip() or similar. But you have to get the data out of XML first.


I merged the data into a manageable structure but you'll need to format the output.

There really isn't anything special here. I simply used linq to join on plan.id == detail.planid and stuck the results in an anonymous object.

var xmaster = XElement.Parse(
    @"<plans>
    <plan>
        <id>1</id>
        <name>Test1</name>
        <description>Description for test 1</description>
    </plan>
    <plan>
        <id>2</id>
        <name>Test2</name>
        <description>Description for test 2</description>
    </plan>
    <plan>
        <id>3</id>
        <name>Test3</name>
        <description>Description for test 3</description>
    </plan>
    </plans>");

var xdetail = XElement.Parse(
    @"<plandetails>
        <detail>
            <planid>1</planid>
            <properties>
                <propery>
                    <name>Pname1</name>
                    <value>Test1Property1</value>
                    <name>Pname2</name>
                    <value>Test1Property2</value>
                    <name>Pname3</name>
                    <value>Test1Property3</value>
                </propery>
            </properties>
        </detail>
        <detail>
            <planid>2</planid>
            <properties>
                <propery>
                    <name>Pname1</name>
                    <value>Test2Property1</value>
                    <name>Pname2</name>
                    <value>Test2Property2</value>
                    <name>Pname3</name>
                    <value>Test2Property3</value>
                </propery>
            </properties>
        </detail>
        <detail>
            <planid>3</planid>
            <properties>
                <propery>
                    <name>Pname1</name>
                    <value>Test3Property1</value>
                    <name>Pname2</name>
                    <value>Test3Property2</value>
                    <name>Pname3</name>
                    <value>Test3Property3</value>
                </propery>
            </properties>
        </detail>   
    </plandetails>");

var merged = from m in xmaster.Descendants("plan")
            join d in xdetail.Descendants("detail")
                on (int)m.Element("id") equals (int)d.Element("planid")
            select new
            {
                Name = m.Element("name").Value,
                Description = m.Element("description").Value,
                Pnames = d.Descendants("propery").Elements().Where(n => n.Name == "value").Select(v => v.Value)
            };

How to merge data in columns in linq

0

精彩评论

暂无评论...
验证码 换一张
取 消