【问题标题】:Please suggest a linq query for my requirement请为我的要求建议一个 linq 查询
【发布时间】:2013-01-23 22:24:16
【问题描述】:

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

表单上有一个复选框..当我们单击它时...根据下面的数据表,它必须根据ItemCode,Sum(SoldQty),StockInHand,LatestRecordValueOfSales,@987654326 进行分组@, 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
========================================================================

【问题讨论】:

标签: linq


【解决方案1】:

一般查询

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

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

【讨论】:

  • 您好,感谢您的回复。它正在部分工作......如何获取其他字符串列,如 Description , UOM 等
  • 与您获得ItemCode 的方式相同(建议这些是Item 的属性),就像我更新的答案一样。执行查询时(例如resQuery.ToList(),它将返回具有您指定属性的匿名对象(由编译器生成)的List
  • 嗨 Mipe34,我快完成了..唯一的错误是当我为 Soldqty 做 Sum 时,它做了两次。我的意思是如果 ItemCode SoldQty 100 2 100 1 所以总和应该是 3..但我得到 6.Other 没问题谢谢。
  • 我在 Ideone 上为您创建了工作示例:ideone.com/xXwgoG 并在我的答案中更正了语法。 PS。你不需要感谢我,只要我的回答对你有帮助就标记为已回答;-)
  • 嗨...Sum(SoldQty) 和 Sum(Amount) 的第一行数据增加了一倍,而其他行则很好
【解决方案2】:

下面是我的代码,它工作正常,但仅对于第一行,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);

【讨论】:

  • 你不应该把你的问题的扩展作为答案。请使用此答案中的代码扩展您的问题,然后删除此帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
相关资源
最近更新 更多