2010-03-04 54 views
1

我的LINQ查詢未在下面產生預期的輸出。基本上,它是table2.cost和table2.code和table2.class的總和,按table1.alias分類並按table1.priority排序。我在DataSet中添加了兩個DataRelation:LINQ - 我如何使用DataSet.DataRelation來加入這些表並總計一個字段?

ds.Relations.Add("Table1Table2", ds.Tables[1].Columns("ID"), ds.Tables[2].Columns("ParentID"); 
ds.Relations.Add("Table2Table3", 
    new DataColumn[] { ds.Tables[2].Columns["Code"], ds.Tables[2].Columns["Class"] },    
    new DataColumn[] { ds.Tables[3].Columns["Code"], ds.Tables[3].Columns["Class"] }); 

var query = from aliases in table1.AsEnumerable() 
    join children in table2.AsEnumerable() on aliases("ID") equals children("ParentID") 
    orderby aliases("priority") 
    select new 
    { 
     Name = aliases("alias"), 
     Cost = 
      from data in table3.AsEnumerable() 
      group data by new { code = data("code"), classcode = data("class") } 
      into datatemp 
      select new 
      { 
       cost = datatemp.Sum(x => x("cost")) 
      } 
    }; 

任何想法我做錯了什麼?提前致謝!

table1: list of aliases and priorities 
----------------- 
alias priority 
alias1 1 
alias2 2 
alias3 4 
alias4 3
table2: children records joined to table1 by ParentID (1-to-many) 
----------------- 
code class ParentID 
code1 class1 1 
code2 class2 1 
code3 class3 2 
code4 class4 4
table3: data, joined to table2 by class and code (1-to-many) 
----------------- 
code class cost 
code1 class1 1.00 
code1 class1 10.00 
code1 class1 26.00 
code2 class2 5.00 
code2 class2 0.00 
code3 class3 1000.00

預期輸出:

alias1 42.00 
alias2 1000.00 
alias4 0.00 
alias3 0.00

回答

0
var query = from x in Table1.AsEnumerable() 
orderby x.Field<int>("Priority") 
select new { 
    Name = x.Field<string>("alias"), 
    TotalCost = x.GetChildRows("Table1Table2") 
      .Sum(c => c.GetChildRows("Table2Table3") 
        .Sum(tx => tx.Field<decimal>("cost"))) 
}; 
相關問題