0

我有以下代码:-

        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();

如果我有另一个包含 2 列“InvNo”和“Amount”的数据表 (DtSet.Tables["tblCost"]),如何在我的第一段代码中添加第五列以恢复每个列的金额总和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();
4

3 回答 3

1

在 SQL 中执行一个查询,将两个表连接起来,并对金额求和

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.

于 2013-01-17T19:49:26.547 回答
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();
于 2013-01-17T20:37:21.097 回答
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)
                });
于 2013-01-17T20:05:15.750 回答