2013-07-02 26 views
3

我有一個觀察的集合競價是類型的拍賣,它看起來像LINQ的GROUPBY歸類,但我怎麼操作集合

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 GroupId,所以我得到3塊一瓦特第i個的GroupId = 1,的GroupId = 2和的GroupId = 3
  • 然後我需要設置一切不是在第1組至零,並將其添加至組1

實施例,

  • 所有以綠色顯示的組1都按原樣複製,然後組1將附加其餘數據,但設置爲零。
  • 同樣,對於第2組和第3組

enter image description here

什麼是通過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;} 
} 
+0

我們應該在哪裏獲得BOD對象的體積和價格?它應該是提供還是出價?或者可能是一個Auction對象應該成爲兩個BOD對象? –

+0

你好,所以BOD對象應該爲每個組創建一個報價和一個報價。所以在上面的例子中,BOD中有6個項目,組1報價,然後組2報價,報價,然後組3報價。 –

回答

2

你可以參加拍賣的清單組ID的唯一列表:

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 
       } 
+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 })) 
} 
+0

需要將結果添加到BOD類型的集合中,該集合的類型與正在應用groupBy的集合的類型不同。聯盟在這種情況下不起作用 –

+0

在聯盟之前做出選擇? – TGlatzer

+0

好主意,之前添加另一個選擇的伎倆。隊友的歡呼聲。 –

1

像這樣的東西應該工作:

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,但這並不難解決。

+0

需要將結果添加到BOD類型的集合中,該集合的類型與正在應用groupBy的集合的類型不同。聯盟在這種情況下不起作用 –

1

我不知道我有你說得對,加上從其他團體項目,但看看這個查詢:

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); 
+0

有趣!這工作。非常好,謝謝。 –