LINQ多列 [英] LINQ multiple columns

查看:250
本文介绍了LINQ多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <根> 
< data1>
< Element1>值< / Element1>
< Element2>值< / Element2>
< Element3>值< / Element3>
< / data1>
< data2>
< Element1>值< / Element1>
< Element2>值< / Element2>
< / data2>
< / root>

从上面的XML中,我想使XML看起来像这样:

 < root> 
< d1e1>值< d1e1>
< d1e2>值< d1e2>
< d2e1>值< d2e1>
< / root>

最有效的方法是什么?
Foreach或Linq在理论上Linq在大多数情况下应该更快,而且速度是这个项目的核心。

有什么想法吗?

$ b

解决方案

这个想法是从一个Y池中选择X个节点,这里的例子被简化为向你展示这个问题。一般来说,就像我有一个多层次的XML,我只需要有一个子层次(aka root + level1),但是从源头上我只需要有一些我感兴趣的元素。



无论如何issiue被解决因为我用foreach做我发现,如果你有一个shema指定的XML,但不能访问LINQ剂量whant无论如何工作。

解决方案是这样的:


  1. 我做了一个函数:

      public System.Xml.XmlElement GetSubElement(XmlElement父元素,字符串元素)
    {
    System。 Xml.XmlElement ret = null;
    if(Parent == null)
    return ret;

    XmlNodeList ContentNodes = Parent.GetElementsByTagName(element);
    if(ContentNodes.Count> 0)
    {
    XmlNode node = ContentNodes.Item(0);
    ret =(XmlElement)节点;
    }

    return ret;

    $ / code $ / pre $
    我在一个重复的区域做了一个foreach循环
  2. p>



  3. 无论如何,它解决了我。



    编辑:
    不知道如何让这个代码正常显示cos Ctrl + K剂量似乎做到了

    <root>
        <data1>
            <Element1>Value</Element1>
            <Element2>Value</Element2>
            <Element3>Value</Element3>
        </data1>
        <data2>
            <Element1>Value</Element1>
            <Element2>Value</Element2>
        </data2>
    </root>
    

    From the above XML I would like to make an XML looking like this:

    <root>
        <d1e1>value<d1e1>
        <d1e2>value<d1e2>
        <d2e1>value<d2e1>
    </root>
    

    What is the most efficient way to process that? Foreach or Linq in theory Linq should be faster in most cases and speed is of the essence for this project

    Any idea?

    解决方案

    The idea was to just select X nodes out of a pool of Y and the example here is simplified to show you the problem. In general it is like that I have a multi level xml that I needed to flat out to only have one sublevel (aka root + level1) but from the source I only need to have certain elements that are of interest to me.

    Anyway the issiue is solved cos I done it with foreach cos I found out that if you have an shema specified in the xml but not accessable LINQ dosent whant to work anyway.

    the solution was like this:

    1. I made a function:

      public System.Xml.XmlElement GetSubElement(XmlElement Parent, string element)
      {
       System.Xml.XmlElement ret = null;
       if (Parent == null)
        return ret;
      
       XmlNodeList ContentNodes = Parent.GetElementsByTagName(element);
       if (ContentNodes.Count > 0)
       {
        XmlNode node = ContentNodes.Item(0);
        ret = (XmlElement)node;
       }
      
       return ret;
      }
      

    2. I made a foreach loop on the area that was repeating

    3. I got the elements that where out of the repeating context with the above function.

    Anyway that solved it for me.

    Edit: Don't know how to get this code to appear properly cos Ctrl+K dosent seem to do it

    这篇关于LINQ多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆