2

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

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

在此处输入图像描述

第二个表(RecEmp)

在此处输入图像描述

 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 查询得到的输出是

在此处输入图像描述

但我不知道如何得到发出数量的总和,也得到接收数量的总和,最后将两者之和作为差异。所需内容如下。

在此处输入图像描述

4

1 回答 1

2

笔记:

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

预期结果:

这就是我得到的:

在此处输入图像描述

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来测试它。

祝你好运!

于 2012-12-23T19:00:28.413 回答