0

任何人都可以为以下要求建议一个 linq 查询。

表单上有一个复选框..当我们单击它时...根据下面的数据表,它必须根据ItemCode, Sum(SoldQty), StockInHand, LatestRecordValueOfSales, Amount,进行分组Description

你不能分组。以下列

  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

类似的问题被问了很多次:

于 2013-01-23T07:06:55.670 回答
0

下面是我的代码,它工作正常,但仅对于第一行,soldqty 和 Amount 值加倍。而其他行数据很好。我无法理解为什么只有第一行数据 Sum(SoldQty) 加倍。

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 回答