简体   繁体   English

如何在C#中结合2个LINQ XML查询

[英]How to combine 2 LINQ XML queries in C#

noob here using LINQ. 菜鸟在这里使用LINQ。 I have created 2 queries that I would like combining if possible. 我创建了两个查询,如果可能的话,我想将它们合并。

        var myData =
        from el in root.Descendants().Elements("sensor")
        where (string)el.Attribute("name") == "Sensor1"
        select el;

        var myData2 =
        from el in myData.Elements("evt")
        select new
        {
            t1 = el.Attribute("time").Value,
            v1 = el.Attribute("val").Value
        };  

        dataGridView1.DataSource = myData2.ToList();

Ideally I'd like to know how to combine the 2 queries. 理想情况下,我想知道如何组合这两个查询。

Thanks 谢谢

It is posible, you could do this. 这是可能的,您可以执行此操作。

var result = root.Descendants()
    .Elements("sensor")
    .Where(el=>(string)el.Attribute("name") == "Sensor1")
    .Elements("evt")
    .Select(el=> new
        {
            t1 = el.Attribute("time").Value,
            v1 = el.Attribute("val").Value
        })
     .ToList()
dataGridView1.DataSource = result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM