2012-12-12 2 views
1

을 위해 내가 XML 다음 한에 의해 -LINQ - Groupy 복잡한 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 등 모든 orderids 각 나라, 각각의 도시를 위해 그 도시의 즉

.

난 내가 그룹 국가와 도시에 수 있어요하는 아래의 쿼리를 사용하고 있지만 해당 국가에 대한 주문을 가져올 수 없습니다입니다 : -

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. (단지의 경우) 구문 분석 여기

var query = from cust in Customers 
      group cust by new { cust.Country, cust.City } into g 
      select new 
      { 
       CountryName = g.Key.Country, 
       CityGroup = g.Key.City, 
       Orders = g.SelectMany(c => c.Orders) 
      }; 

: 그리고 당신은 고객의 정확한 분석을 할 경우 같은

+0

우선, XML과는 아무런 관련이 없습니다. LINQ 개체 목록을 쿼리하십시오. 그러므로 우리가 getcustomerdata 메소드를 사용하여 클래스 선언을 제공하면 도움이됩니다. –

답변

4

, 다음 쿼리는 같아야합니다 나는 다음과 같은 클래스를 사용

private List<Customer> GetCustomerData() 
{    
    XDocument xdoc = XDocument.Load(path_to_xml); 
    return xdoc.Descendants("customer") 
       .Select(c => new Customer() 
       { 
        Id = (string)c.Element("id"), 
        Name = (string)c.Element("name"), 
        Address = (string)c.Element("address"), 
        Country = (string)c.Element("country"), 
        City = (string)c.Element("city"), 
        Orders = c.Descendants("order") 
           .Select(o => new Order() 
           { 
            Id = (int)o.Element("id"), 
            Date = (DateTime)o.Element("orderdate"), 
            Total = (decimal)o.Element("total") 
           }).ToList() 
       }).ToList(); 
} 

(없이 우편 번호, 팩스 번호, 전화 번호)

public class Customer 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string Country { get; set; } 
    public string City { get; set; } 
    public List<Order> Orders { get; set; } 
} 

public class Order 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
    public decimal Total { get; set; } 
} 
+0

좋은 것 같지만 테스트하지 않고도 LINQ를 쓸 수 없으므로 테스트 한 후에 +1 할 것입니다 :) –

+1

예 +1, 'SelectMany'의 위대한 사용. 나는 또 다른 +1을 추가 할 수 있으면 좋겠다. 쿼리는 정말 멋지다. :) –

+0

@RomanPekar 의견이 너무 분분하다. –