2

私の質問をする前に、いくつかの背景を説明します。

私はSQLコンパクトを使用しており、2つのテーブルがあります。最初のテーブル(IssueEmp)

ここに画像の説明を入力

2 番目のテーブル (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クエリから得られる出力は次のとおりです

ここに画像の説明を入力

しかし、発行された数量の合計と受け取った数量の合計を取得する方法がわかりません。最後に、2つの合計の差を差分として取得します。必要なものは以下。

ここに画像の説明を入力

4

1 に答える 1

2

ノート:

私は少し怠け者なので、提供されたすべてのレコードは使用せず、最初の 4 つのレコードのみを使用しました。

期待される結果:

これは私が得たものです:

ここに画像の説明を入力

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 に答える