私は次のXMLを持っています:-
<customer>
<id>ALFKI</id>
<name>Alfreds Futterkiste</name>
<address>Obere Str. 57</address>
<city>Berlin</city>
<postalcode>12209</postalcode>
<country>Germany</country>
<phone>030-0074321</phone>
<fax>030-0076545</fax>
<orders>
<order>
<id>10643</id>
<orderdate>1997-08-25T00:00:00</orderdate>
<total>814.50</total>
</order>
<order>
<id>10692</id>
<orderdate>1997-10-03T00:00:00</orderdate>
<total>878.00</total>
</order>
</orders>
</customer>
次の形式でデータを取得したい:-国名:ドイツ都市名:ベルリン注文:orderid1、orderid2 .. ..
都市名:マンハイムの注文:orderid1、orderid2など。
つまり、各国、それぞれの都市、およびその都市のすべてのorderidです。
以下のクエリを使用して、国とその都市をグループ化できますが、その国の注文を取得できません:-
List<Customer> Customers = GetCustomerData();
var query = (from cust in Customers
from ord in cust.Orders
group cust by cust.Country into CountryGroup
select new
{
CountryName = CountryGroup.Key,
CityGroup = (from c in CountryGroup
where c.Country == CountryGroup.Key
group c by c.City into CityGroup
select new
{
CityName = CityGroup.Key
})
});
GetCustomerDataのコード:-
CustomerList = (from e in XDocument.Load(@"D:\Console Application\LINQDemo\GroupingOperators\GroupingOperators\XMLFiles\Customers.xml").Root.Elements("customer")
select new Customer
{
CustomerID = (string)e.Element("id"),
CustomerName = (string)e.Element("name"),
Address = (string)e.Element("address"),
City = (string)e.Element("city"),
Region = (string)e.Element("region"),
PostalCode = (string)e.Element("postalcode"),
Country = (string)e.Element("country"),
PhoneNo = (string)e.Element("phone"),
Fax = (string)e.Element("fax"),
Orders = (from o in e.Element("orders").Elements("order")
select new Order
{
OrderID = (int)o.Element("id"),
OrderDate = (DateTime)o.Element("orderdate"),
OrderTotal = (decimal)o.Element("total")
}).ToArray()
}).ToList();
助けてください!
TIA。