【问题标题】:Update record based on group by根据分组更新记录
【发布时间】:2017-03-21 03:42:14
【问题描述】:

我有一个行列表:

 public class Row 
{
    public string SINo{ get; set; }
    public string AccountName { get; set; }
    public string ProductCode { get; set; }
    public int Quantity { get; set; }
    public int FreeGoods { get; set; }
    ...

}

还有这些记录:

  SINo          AccountName       ProductCode       Quantity       FreeGoods
S145144       DUMAN, MARILYN M.    13002154            8              
S145144       DUMAN, MARILYN M.    13002154            2
S144413        CRUZ, ISABELLA      13002154            2
S145151        CASTRO, MARIO C.    13002154            5
S145151        CASTRO, MARIO C.    13002154            1
S142058      RILLERA, FREDILYN B.  13002155            10
S142058      RILLERA, FREDILYN B.  13002155            2

如您所见,原始订单的下一行记录相同,但数量不同。这意味着 Quantity 的另一行是 FreeGoods。但在其他一些行中,也可能存在没有针对 FreeGoods 的后续记录的记录。我对linq不太了解。如何分组并选择它来制作这样的东西?

  SINo          AccountName       ProductCode       Quantity      FreeGoods
S145144       DUMAN, MARILYN M.    13002154            8             2
S144413        CRUZ, ISABELLA      13002154            2
S145151        CASTRO, MARIO C.    13002154            5             1
S142058      RILLERA, FREDILYN B.  13002155            10            2

每条记录都没有唯一的属性。属性例如SINo、AccountName、Product 每行可以重复。

【问题讨论】:

  • 是否总是按这个顺序排列两行?展示你到目前为止所做的尝试。
  • 您确定每个产品代码都有两条记录,其中第二条记录包含免费商品。
  • @Nkosi 按这个顺序是的,但它可以在其他一些行中重复(例如,客户再次订购了产品)。我也更新了我的问题
  • @jitender 是的,不是那么准确。但是有些记录没有免费商品的下一个记录。我会更新的

标签: c# .net linq group-by


【解决方案1】:

以下 Linq 查询给了我预期的结果。希望对你有帮助

List<Row> list = new List<Row>();
//add data to list
//then execute this linq query
list.GroupBy(x => x.SINo).Select(y=> new Row
            {
                AccountName = y.First().AccountName,
                ProductCode = y.First().ProductCode,
                Quantity = y.First().Quantity,
                SINo = y.First().SINo,
                FreeGoods = y.Count() > 1 ? y.Last().Quantity : 0
            });

【讨论】:

    【解决方案2】:

    我建议您添加一个新列 hasFreeGoods,因为这将为您提供更干净的解决方案

    在当前结构上。我假设每个产品最多有两条记录

    var groups=from r in Rows group r by new {
    r.ProductCode,
    r.SINo,
    r.AccountName
    } into gr;
    
    groups.select(g => new {
    g.Key.ProductCode,
    g.Key.SINo,
    g.Key.AccountName,
    Quantity=g.First().Quantity,
    FreeGoods =g.Count()>1 ? g.Last().Quentity :0   
    })
    

    【讨论】:

      【解决方案3】:

      你可以试试这个。与之前的答案略有不同:

       var result = list.GroupBy(p => p.SINo,                             
                                   (key, g) => new { 
                                   SINo = g.Key,
                                   ProductCode = g.First().ProductCode,
                                   AccountName = g.First().AccountName,
                                   Quantity = g.First().Quantity,
                                   FreeGoods = g.Count() > 1 ? g.Last().Quantity : 0 
                                   }).ToList();  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        相关资源
        最近更新 更多