Linq返回来自不同数据表的求和值

时间:2013-01-17 19:34:54

标签: c# linq datatable

我有以下代码: -

        var distinctValues = DtSet.Tables["tblData"].AsEnumerable()
                    .Select(row => new
                    {
                        name_1 = "1",
                        name_2 = "I",
                        name_3 = row.Field<string>("EorD"),
                        name_4 = row.Field<string>("InvNo")
                    })
                    .Distinct();

如果我有另一个数据表(DtSet.Tables [“tblCost”])有2列,“InvNo”和“Amount”,我怎么能在我的第一段代码中添加第五列来带回总和tblCost上每个InvNo的金额?喜欢: -

        var distinctValues = DtSet.Tables["tblData"].AsEnumerable()
                    .Select(row => new
                    {
                        name_1 = "1",
                        name_2 = "I",
                        name_3 = row.Field<string>("EorD"),
                        name_4 = row.Field<string>("InvNo")
                        name_5 = **Summed total of Amount column on tblCost per InvNo**
                    })
                    .Distinct();

3 个答案:

答案 0 :(得分:1)

在SQL中执行连接两个表并对ammount

求和的查询
SELECT
    D.EorD,
    D.InvNo,
    SUM(C.Amount) As TotalAmount
FROM
    tblData D
    LEFT JOIN tblCost C
        ON D.InvNo = C.InvNo
GROUP BY
    D.EorD, D.InvNo;

如果tblCost中没有相应的记录,我使用了左连接。

注意:我不确定专栏EorD。我认为InvNo中的tblData是唯一的。否则,如果tblCost中有相应的列,则应将其添加到联接中,或者将其从GROUP BY中删除,并在MAX(D.EorD) AS EorD上应用聚合函数。

答案 1 :(得分:1)

您可以执行“群组加入”并在一个查询中执行此操作:

var distinctValues = 
    (from data in DtSet.Tables["tblData"].AsEnumerable()
     join cost in DtSet.Tables["tblCost"].AsEnumerable().Where(r=>!r.IsNull("Amount"))
        on data.Field<string>("InvNo") equals cost.Field<string>("InvNo") 
            into dc
    select new
    {
        name_1 = "1",
        name_2 = "I",
        name_3 = data.Field<string>("EorD"),
        name_4 = data.Field<string>("InvNo"),
        name_5 = dc.Sum(d=>d.Field<int>("Amount"))
    })
    .Distinct();

答案 2 :(得分:0)

您应该加入您的馆藏,将其分组并汇总:

        var tblDataValues = DtSet.Tables["tblData"].AsEnumerable()
                                .Select(row => new
                                {
                                    name_1 = "1",
                                    name_2 = "I",
                                    name_3 = row.Field<string>("EorD"),
                                    name_4 = row.Field<string>("InvNo")
                                })
                                .Distinct();;
        var tblCostValues = DtSet.Tables["tblCost"].AsEnumerable()
                                .Select(row => new
                                {
                                    InvNo = row.Field<string>("InvNo"),
                                    Amount = row.Field<int>("Amount"),
                                }
            );


        var joinedValue = tblDataValues.Join(tblCostValues, v => v.name_4, c => c.InvNo, (v, c) => new { c, v })
                .GroupBy(k => k.v, c => c.c)
                .Select(g => new
                {
                    g.Key.name_1,
                    g.Key.name_2,
                    g.Key.name_3,
                    g.Key.name_4,
                    name_5 = g.Aggregate(0, (s, c) => s + c.Amount)
                });