首页 > c# > LINQ to XML查询

LINQ to XML查询 (LINQ to XML query)

2010-08-23 c#linq-to-xml

问题

我有两个xml文件。

第一个company.xml:

<company active="1">
    <name>name1</name>
    <location>first floor</location>
    <room>25</room>
    <category>power,gas,water</category>
</company>

<company active="1">
    <name>name2</name>
    <location>second floor</location>
    <room>23</room>
    <category>water,gas</category>
</company>

第二个bills.xml:

<bill>
    <name>bill1</name>
    <category>power</category>
    <total>5432</total>
</bill>

<bill>
    <name>bill2</name>
    <category>power</category>
    <total>1200</total>
</bill>

<bill>
    <name>bill2</name>
    <category>gas</category>
    <total>3464</total>
</bill>

现在我有这个查询,我在哪里按company name元素分组xml ,并总结来自的总值bills

XDocument fDoc = XDocument.Load("company.xml");
XDocument rDoc = XDocument.Load("bills.xml");


var query = from f in fDoc.Elements("company")
where ((string)f.Attribute("active")).Equals("1")
orderby f.Element("name").Value
from r in racuniRoot.Elements("bill")
where (f.Element("category").Value).Split(',').Contains(r.Element("category").Value)
group new
{
BillTotal = Convert.ToInt32(r.Element("total").Value)
}
by f.Element("name").Value into g
select new
{
Name = g.Key,
Total = g.Sum(rec =>rec.BillTotal)
};

foreach (var k in query)
{
    litList.Text += k.Name + k.Total;
}

所以这个查询的结果是:

name1 6632

name2 3464

这没关系,但我如何在此查询中选择其他company元素(locationroom)?

这是我想要的最终结果:

name1 6632一楼25

name2 3464二楼23

我怎样才能做到这一点?

谢谢!

解决方法

根据姓名,地点和房间进行分组

var query = from f in fDoc.Descendants("company")
                    where ((string)f.Attribute("active")).Equals("1")
                    orderby f.Element("name").Value
                    from r in rDoc.Descendants("bill")
                    where (f.Element("category").Value).Split(',').Contains(r.Element("category").Value)
                    group new
                    {
                        BillTotal = Convert.ToInt32(r.Element("total").Value)
                    }
                    by f.Element("name").Value + f.Element("location").Value + f.Element("room").Value into g
                    select new
                    {
                        Name = g.Key,
                        Total = g.Sum(rec => rec.BillTotal),
                    };

问题

I have two xml files.

First company.xml:

<company active="1">
    <name>name1</name>
    <location>first floor</location>
    <room>25</room>
    <category>power,gas,water</category>
</company>

<company active="1">
    <name>name2</name>
    <location>second floor</location>
    <room>23</room>
    <category>water,gas</category>
</company>

Second bills.xml:

<bill>
    <name>bill1</name>
    <category>power</category>
    <total>5432</total>
</bill>

<bill>
    <name>bill2</name>
    <category>power</category>
    <total>1200</total>
</bill>

<bill>
    <name>bill2</name>
    <category>gas</category>
    <total>3464</total>
</bill>

Now i have this query where i'm grouping xml by company name element, and summing total value from bills

XDocument fDoc = XDocument.Load("company.xml");
XDocument rDoc = XDocument.Load("bills.xml");


var query = from f in fDoc.Elements("company")
where ((string)f.Attribute("active")).Equals("1")
orderby f.Element("name").Value
from r in racuniRoot.Elements("bill")
where (f.Element("category").Value).Split(',').Contains(r.Element("category").Value)
group new
{
BillTotal = Convert.ToInt32(r.Element("total").Value)
}
by f.Element("name").Value into g
select new
{
Name = g.Key,
Total = g.Sum(rec =>rec.BillTotal)
};

foreach (var k in query)
{
    litList.Text += k.Name + k.Total;
}

So the result with this query is:

name1 6632

name2 3464

And that is ok, but how do i select other company elements (location and room) in this query?

This is what i want for final result:

name1 6632 first floor 25

name2 3464 second floor 23

How can i do this?

Thanks!

解决方法

do a group by on name, location and room

var query = from f in fDoc.Descendants("company")
                    where ((string)f.Attribute("active")).Equals("1")
                    orderby f.Element("name").Value
                    from r in rDoc.Descendants("bill")
                    where (f.Element("category").Value).Split(',').Contains(r.Element("category").Value)
                    group new
                    {
                        BillTotal = Convert.ToInt32(r.Element("total").Value)
                    }
                    by f.Element("name").Value + f.Element("location").Value + f.Element("room").Value into g
                    select new
                    {
                        Name = g.Key,
                        Total = g.Sum(rec => rec.BillTotal),
                    };
相似信息