LINQ - 如何使用 DataSet.DataRelation 来连接这些表并对一个字段求​​和?

发布于 2024-08-24 05:59:11 字数 1636 浏览 9 评论 0原文

我的 LINQ 查询没有产生下面的预期输出。基本上,它是与 table2.code 和 table2.class 对应的 table3.cost 的总和,按 table1.alias 分类并按 table1.priority 排序。我向数据集中添加了两个 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

My LINQ query is not producing the expected output below. Basically, it's the sum of table3.cost corresponding to table2.code and table2.class categorized by table1.alias and ordered by table1.priority. I added two DataRelation's to the DataSet:

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"))
            }
    };

Any ideas what I'm doing wrong? Thanks in advance!

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

Expected output:

alias1 42.00
alias2 1000.00
alias4 0.00
alias3 0.00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-08-31 05:59:11
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")))
};
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")))
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文