使用LINQ的字段总和

时间:2012-12-23 15:35:48

标签: c# linq sql-server-ce

在问我的问题之前有一些背景知识。

我使用sql compact,我有两个表, 第一个表(IssueEmp)

enter image description here

第二张表(RecEmp)

enter image description here

 SqlCeDataAdapter adap = new SqlCeDataAdapter("SELECT * FROM RecEmp", cn);
            DataTable dat = new DataTable();
            DataSet receice = new DataSet();
            adap.Fill(receice);
            adap.Fill(dat);

 SqlCeDataAdapter adap1 = new SqlCeDataAdapter("SELECT * FROM IssueEmp", cn);
            DataTable dat1 = new DataTable();
            DataSet issue = new DataSet();
            adap1.Fill(issue);
            adap1.Fill(dat1);

我正在使用linq

在RecEmp和IssueEmp之间执行连接
var res = from t1 in receice.Tables[0].AsEnumerable()

                      join t2 in issue.Tables[0].AsEnumerable()                                            
                         on new 
                         {
                             CNo = t1.Field<int>("CNo"),
                             Empid = t1.Field<int>("EmpID") 
                         } 
                         equals new 
                         {
                             CNo = t2.Field<int>("CNo"),
                             Empid = t2.Field<int>("EmpID") 
                         }                     
                      select new
                       {
                           SNo = t1.Field<int>("SNo"),
                           ChNo = t1.Field<int>("CNo"),
                           EmpID = t1.Field<int>("EmpID"),
                           DateIssued = t2.Field<DateTime>("Date"),                                                                               
                           RMIssued = t2.Field<string>("RMCode"),
                           QuantityIssued = t2.Field<double>("Quantity"),

                           DateReceived = t1.Field<DateTime>("Date"),
                           RMCodeReceived = t1.Field<string>("RMCode"),
                           QuantityReceived = t1.Field<double>("Quantity")

                       };

从上面的linq查询获得的输出是

enter image description here

但是我不知道如何获得发行数量的总和同样是接收数量的总和,最后两个总和之间的差异作为差异。要求如下。

enter image description here

1 个答案:

答案 0 :(得分:2)

注意:

我有点懒,所以我没有使用你提供的所有记录,只有前四个记录。

预期结果:

这就是我得到的:

enter image description here

Linq查询:

var query = from d in data
           group d by new { d.DateIssued, d.EmpId, d.ChNo, d.DateReceived }
           into x
           select new {
                Date = x.Key.DateIssued,
                CNo = x.Key.ChNo,
                EmpId=x.Key.EmpId,
                CRi = x.Where(c=>c.RMIssued == "CR").Sum(c=>c.QuantityIssued),
                SJi = x.Where(c=>c.RMIssued == "SJ").Sum(c=>c.QuantityIssued),
                TTi = x.Where(c=>c.RMIssued == "TT").Sum(c=>c.QuantityIssued),
                WRi = x.Where(c=>c.RMIssued == "WR").Sum(c=>c.QuantityIssued),
                TotalIssued = x.Sum(c => c.QuantityIssued),

                DateReceived = x.Key.DateReceived,
                CRr = x.Where(c=>c.RMCodeReceived == "CR").Sum(c=>c.QuantityReceived),
                SJr = x.Where(c=>c.RMCodeReceived == "SJ").Sum(c=>c.QuantityReceived),
                TTr = x.Where(c=>c.RMCodeReceived == "TT").Sum(c=>c.QuantityReceived),
                WRr = x.Where(c=>c.RMCodeReceived == "WR").Sum(c=>c.QuantityReceived),
                TotalReceived = x.Sum(c => c.QuantityReceived),

                Diff = x.Sum(c => c.QuantityIssued) - x.Sum(c => c.QuantityReceived)
            };

使用的数据:

这是我用来测试它的数据集:

var data= new []{
    new { SNo= 9,  ChNo=5,  EmpId=81, DateIssued=dateIssued, RMIssued="SJ", QuantityIssued=30,   DateReceived=dateReceived, RMCodeReceived="SJ", QuantityReceived=20.3},
    new { SNo= 10, ChNo=5,  EmpId=81, DateIssued=dateIssued, RMIssued="SJ", QuantityIssued=30,   DateReceived=dateReceived, RMCodeReceived="CR", QuantityReceived=9.6},
    new { SNo= 11, ChNo=28, EmpId=82, DateIssued=dateIssued, RMIssued="TT", QuantityIssued=30.5, DateReceived=dateReceived, RMCodeReceived="TT", QuantityReceived=29},
    new { SNo= 12, ChNo=28, EmpId=82, DateIssued=dateIssued, RMIssued="WR", QuantityIssued=10,   DateReceived=dateReceived, RMCodeReceived="TT", QuantityReceived=29}
};

我建议您使用LinqPad进行测试。

祝你好运!