0

以下の要件を満たす linq クエリを提案できる人はいますか。

フォームにチェックボックスがあります..クリックすると...以下のデータテーブルに従って、、、、、、、に従ってグループ化するItemCode必要Sum(SoldQty)StockInHandありLatestRecordValueOfSalesます。AmountDescription

グループ化はできません。次の列

  1. solddate- 最新の販売日を表示
  2. department
  3. category

ItemCode Description UOM SoldQty Stock in Hand SellPrice Amount
--------------------------------------------------------------- 

100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/21/2013 MEAT INDIAN BEAF 
100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/21/2013 MEAT INDIAN BEAF 
200 frozen meat Kilograms 0.005 88.19 4 4.01 0 200 1/21/2013 OTHERS INDIAN BEAF 
200 frozen meat Kilograms 0.044 88.19 4 4.04 0 200 1/21/2013 OTHERS INDIAN BEAF 
100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/22/2013 MEAT INDIAN BEAF 
200 frozen meat Kilograms 0.054 88.19 4 4.05 0 200 1/22/2013 OTHERS INDIAN BEAF 
200 frozen meat Kilograms 0.055 88.19 4 4.06 0 200 1/22/2013 OTHERS INDIAN BEAF
========================================================================
4

2 に答える 2

0

一般的なクエリ

var resQuery = from i in someQueryable
              group i by new {i.groupProperty1, i.groupProperty2} into g
                          select new
                          {
                              Property1 = g.Key.Property1,
                              Property2 = g.Key.Property2
                              Total = g.Sum(p => p.SumProperty),
                              /// other properties
                          };

サンプルデータの場合、次のようになります。

var resQuery = from i in dbContext.Items
              group i by new{ i.ItemCode, i.Description, i.UOM} into g
                          select new
                          {
                              ItemCode = g.Key.ItemCode,
                              TotalSold = g.Sum(p => p.SoldQty),
                              Description = g.Key.Description,
                              UOM =g.Key.UOM
                              /// other properties
                          };

Ideone で例を試す: http://ideone.com/xXwgoG

SOで何度も尋ねられた同様の質問:

于 2013-01-23T07:06:55.670 に答える
0

以下は私のコードで、正常に動作しますが、soldqty と Amount の値が 2 倍になっているのは最初の行だけです。他の行のデータは問題ありません。

decimal? SoldQty, stockinhand,SellPrice,Amount,CostPrice;
 string ItemCode, Description,UOM,BarCode,SoldDate,Department,Category,User;
  var resQuery = from row in dtFilter.AsEnumerable()
group row by row.Field<string>("Item Code") into g
 select dtFilter.LoadDataRow(new object[]
 {
 ItemCode=g.Key,
 Description=g.Select(r=>r.Field<string>("Description")).First<string>(),
 UOM=g.Select(r=>r.Field<string>("UOM")).First<string>(),  
 SoldQty = g.Sum(r => r.Field<decimal?>("Sold Qty")).Value,
 stockinhand=g.Select(r=>r.Field<decimal?>("Stock in Hand")).First<decimal?>(),
 SellPrice=g.Select(r=>r.Field<decimal?>("Sell Price")).First<decimal?>(),
 Amount = g.Sum(r => r.Field<decimal?>("Amount")).Value,
 CostPrice = g.Sum(r => r.Field<decimal?>("Cost Price")).Value,
 BarCode=g.Select(r=>r.Field<string>("Barcode")).First<string>(),
 SoldDate=g.Select(r=>r.Field<string>("SoldDate")).Last<string>(),
 Department=g.Select(r=>r.Field<string>("Department")).First<string>(),
 Category=g.Select(r=>r.Field<string>("Category")).First<string>(),
 User=g.Select(r=>r.Field<string>("User")).First<string>(),  }, false);
于 2013-01-23T14:24:16.590 に答える