【问题标题】:Linq Groupby categorizes but how do I manipulate the values in the collectionLinq Groupby 分类,但我如何操作集合中的值
【发布时间】:2013-07-03 15:09:54
【问题描述】:

我有一个可观察的 Auctions 集合,它的 Auction 类型看起来像

public class Auction
{
     public int GroupId {get; set;}
     public DateTime Date {get; set;}
     public string Hour {get; set;}
     public decimal Offer {get; set;}
     public decimal Bid {get; set;}
     public decimal OfferPrice {get; set;}
     public decimal BidPrice {get; set;}
}

该集合包含以下数据,

Group Id    Date    Hour    Offer   Bid Offer Price Bid Price
1   27/06/2013  00:00   5.556   86.3    250 0
1   27/06/2013  00:15   0   0   250 0
1   27/06/2013  00:30   0   0   250 0
1   27/06/2013  00:45   0   0   250 0
1   27/06/2013  01:00   0   0   250 0
1   27/06/2013  01:15   5.556   86.3    250 0
2   27/06/2013  01:30   8.68    19.9    100 20
2   27/06/2013  01:45   0   0   100 20
2   27/06/2013  02:00   8.68    19.9    100 20
2   27/06/2013  02:15   0   0   100 20
2   27/06/2013  02:30   8.68    19.9    100 20
2   27/06/2013  02:45   0   0   100 20
2   27/06/2013  03:00   8.68    19.9    100 20
3   27/06/2013  03:15   87.14   87.1    150 0
3   27/06/2013  03:30   0   0   150 0
3   27/06/2013  03:45   0   0   150 0
3   27/06/2013  04:00   0   0   150 0
3   27/06/2013  04:15   0   0   150 0
3   27/06/2013  04:30   0   0   150 0
3   27/06/2013  04:45   0   0   150 0
3   27/06/2013  05:00   0   0   150 0

现在我要做的是,

  • 按 GroupId 分组集合,所以我得到 3 个块,其中一个 GroupId = 1、GroupId =2 和 GroupId = 3。
  • 然后我需要将不在第 1 组中的所有内容设置为零并将其添加到第 1 组。

例子,

  • 绿色组 1 中的所有内容都按原样复制,然后将组 1 附加其余数据但设置为零。
  • 第 2 组和第 3 组也是如此。

通过 Linq 执行此操作的最佳方法是什么?

需要添加结果的集合是BOD类型,它包含

Public class BOD
{
    public DateTime Date {get; set;}
    public string Time {get; set;}
    public decimal Volume {get; set;}
    public decimal Price {get; set;}
}

【问题讨论】:

  • 我们应该从哪里获得 BOD 对象的数量和价格?应该是报价还是投标?还是一个 Auction 对象应该变成两个 BOD 对象?
  • 您好,所以 BOD 对象应该为每个组创建一个报价和一个投标。所以在上面的例子中,BOD 中有 6 个项目,第 1 组报价,投标,然后第 2 组报价,投标,然后第 3 组报价,投标。

标签: c# .net linq


【解决方案1】:

您可以将拍卖列表加入到唯一的 GroupId 列表中:

var q = (from a in Auctions
         from g in (Auctions.Select(aa=>aa.GroupId).Distinct())
         orderby g, a.Date, a.Hour
         select new Auction
             {
                  GroupId = g,
                  Date = a.Date,
                  Hour = a.Hour,
                  Offer      = a.GroupId == g ? a.Offer : 0,
                  Bid        = a.GroupId == g ? a.Bid : 0,
                  OfferPrice = a.GroupId == g ? a.OfferPrice : 0,
                  BidPrice   = a.GroupId == g ? a.BidPrice : 0
              }

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    var list = new List<YourType>(); // Your data source
    var groupedList = list.GroupBy(listEntry => listEntry.GroupId);
    
    foreach(var groupList in groupedList)
    {
       var unionList = groupList.ToList()
           .Union(list.Where(listEntry => listEntry.GroupId != groupList.Key)
           .Select(listEntry => new YourType { GroupId = groupList.Key, a = 0, b = 0, c = listEntry.c }))
    }
    

    【讨论】:

    • 需要将结果添加到BOD类型的集合中,该集合的类型与应用groupBy的集合不同。在这种情况下,联合将不起作用
    • 好主意,在之前添加另一个选择就可以了。干杯伙伴。
    【解决方案3】:

    这样的事情应该可以工作:

    var group1 =
                (from a in auctions where a.GroupId == 1 select a).Union(
                    from a in auctions
                    where a.GroupId != 1
                    select new Auction { GroupId = 1, Date = a.Date, Hour = a.Hour });
    

    除非您专门复制它们,否则新的拍卖将包含零。您希望摆脱硬编码的 id,但这并不难解决。

    【讨论】:

    • 需要将结果添加到BOD类型的集合中,该集合的类型与应用groupBy的集合不同。在这种情况下,联合将不起作用
    【解决方案4】:

    我不确定我是否正确地添加了来自其他组的项目,但请查看以下查询:

    var auctions = new List<Auction>
    {
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "00:00", Offer = 5.556m, Bid = 86.3m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "00:15", Offer = 0m, Bid = 0m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "00:30", Offer = 0m, Bid = 0m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "00:45", Offer = 0m, Bid = 0m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "01:00", Offer = 0m, Bid = 0m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 1, Date = DateTime.Parse("27/06/2013"), Hour = "01:15", Offer = 5.556m, Bid = 86.3m, OfferPrice = 250m, BidPrice = 0m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "01:30", Offer = 8.68m, Bid = 19.9m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "01:45", Offer = 0m, Bid = 0m, OfferPrice = 250m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "02:00", Offer = 8.68m, Bid = 19.9m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "02:15", Offer = 0m, Bid = 0m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "02:30", Offer = 8.68m, Bid = 19.9m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "02:45", Offer = 0m, Bid = 0m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 2, Date = DateTime.Parse("27/06/2013"), Hour = "03:00", Offer = 8.68m, Bid = 19.9m, OfferPrice = 100m, BidPrice = 20m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "03:15", Offer = 87.14m, Bid = 87.1m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "03:30", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "03:45", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "04:00", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "04:15", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "04:30", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "04:45", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m},
        new Auction {GroupId = 3, Date = DateTime.Parse("27/06/2013"), Hour = "05:00", Offer = 0m, Bid = 0m, OfferPrice = 150m, BidPrice = 0m}
    };
    
    // Create 2 BOD object for each Auction
    var b1 =
            from a in auctions
            from b in new List<BOD> {
                    new BOD { Date = a.Date, Time = a.Hour, Price = a.BidPrice, Volume = a.Bid },
                    new BOD { Date = a.Date, Time = a.Hour, Price = a.OfferPrice, Volume = a.Offer } 
            }
            select b;
    
    // Create 2 BOD object with zero price and volume for each Auction with another GroupId
    var b2 =
            from d in auctions.Select(x => x.GroupId)
            from a in auctions.Where(y => y.GroupId != d)
            from b in new List<BOD> {
                    new BOD { Date = a.Date, Time = a.Hour, Price = 0, Volume = 0 },
                    new BOD { Date = a.Date, Time = a.Hour, Price = 0, Volume = 0 } 
            }
            select b;
    
    var bods = b1.Union(b2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2016-03-16
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多