2010-05-06 120 views
0

我有一個數據表,看起來像這樣。SQL到XML使用LINQ - 嵌套集合

name, hour, price1, price2, price3, price4, price5 
fred, 3, 12.5, 13.5, 14, 15, 16 
dave, 6, 8, 12, 18, 20.2, 25 
fred, 6, 10, 11, 14, 15, 19.7 

此表需要輸出到一個xml文件,看起來像這樣。

<timeCost> 
    <person name="fred"> 
    <time hour="5"> 
     <cost price="12.5" /> 
     <cost price="13.5" /> 
     <cost price="14" /> 
     <cost price="15" /> 
     <cost price="16" /> 
    </time> 
    <time hour="6"> 
     <cost price="10" /> 
     <cost price="11" /> 
     <cost price="14" /> 
     <cost price="15" /> 
     <cost price="19.7" /> 
    </time> 
    </person> 
    <person name="dave"> 
    <time hour="6"> 
     <cost price="8" /> 
     <cost price="12" /> 
     <cost price="18" /> 
     <cost price="20.2" /> 
     <cost price="25" /> 
    </time> 
    </person> 
</timeCost> 

我有一個LINQ查詢來從SQL這樣的數據這樣的事情。

// initialize data context 
var people = from p in dc.people orderby p.name, p.hour select p; 

但是,我在使用linq(csharp)編寫xml時遇到了麻煩。具體來說,問題是我不知道如何處理每個名稱節點下的多個時間節點(嵌套循環/集合)。如何才能做到這一點?

這是一個sql 08 ent db如果它對任何人都很重要。

回答

1

您應該在該人的姓名上創建一個分組,也可能將價格選擇到列表中。像下面的模型應該工作。

class Person 
{ 
    public string Name { get; set; } 
    public int Hour { get; set; } 
    public decimal Price1 { get; set; } 
    public decimal Price2 { get; set; } 
    public decimal Price3 { get; set; } 
    public decimal Price4 { get; set; } 
    public decimal Price5 { get; set; } 
} 

...

List<Person> people = new List<Person>() 
{ 
    new Person() { Name = "Fred", Hour = 3, Price1 = 12.5m, Price2 = 13.5m, Price3 = 14, Price4 = 15, Price5 = 16 }, 
    new Person() { Name = "Fred", Hour = 6, Price1 = 10, Price2 = 11, Price3 = 14, Price4 = 15, Price5 = 19.7m }, 
    new Person() { Name = "Dave", Hour = 6, Price1 = 8, Price2 = 12, Price3 = 18, Price4 = 20.2m, Price5 = 25 } 
}; 

var query = from p in people 
      group p by p.Name into pg 
      select new 
      { 
       Name = pg.Key, 
       Records = from record in pg 
          select new 
          { 
           Hour = record.Hour, 
           Prices = new List<Decimal>() { record.Price1, record.Price2, record.Price3, record.Price4, record.Price5 } 
          } 
      }; 

XDocument document = new XDocument(new XElement("timeCost", 
           from person in query 
           select new XElement("person", new XAttribute("name", person.Name), 
            from record in person.Records 
            select new XElement("time", new XAttribute("hour", record.Hour), 
             from price in record.Prices 
             select new XElement("cost", new XAttribute("price", price)))))); 

的的XDocument現在看起來是這樣

<timeCost> 
    <person name="Fred"> 
    <time hour="3"> 
     <cost price="12.5" /> 
     <cost price="13.5" /> 
     <cost price="14" /> 
     <cost price="15" /> 
     <cost price="16" /> 
    </time> 
    <time hour="6"> 
     <cost price="10" /> 
     <cost price="11" /> 
     <cost price="14" /> 
     <cost price="15" /> 
     <cost price="19.7" /> 
    </time> 
    </person> 
    <person name="Dave"> 
    <time hour="6"> 
     <cost price="8" /> 
     <cost price="12" /> 
     <cost price="18" /> 
     <cost price="20.2" /> 
     <cost price="25" /> 
    </time> 
    </person> 
</timeCost>