我有一個List<ShipmentInformation>
LINQ總結和的GroupBy確定正確的數據
public class ShipmentInformation
{
public string Type { get; set; }
public long StartID { get; set; }
public long EndID { get; set; }
public DateTime BoxDate { get; set; }
}
我現在有這樣的代碼,以確定最庫存:
var TypeTotals = shipmentInfo.GroupBy(x => x.Type).Select(x => new { Type = x.Key, Total = x.Sum(y => (y.EndID - y.StartID) + 1) });
//Select the one with the largest amount of stock
var LargestType = TypeTotals.Aggregate((l, r) => l.Total > r.Total ? l : r).Chip;
但是,如果總數是完全一樣的它將選擇TypeTotals
中的最後一項,因此我現在要添加一個檢查以確保使用最早的BoxDate
。
所以我們可以說我有10個A型項目和10個B型項目,此刻會選擇B型。
我想現在確保當我返回LargestType
時,它返回該類型的最早的項目。因此,如果A中的任何項目的BoxDate早於B中的任何項目,則應選擇A.
非常感謝! – Jon 2012-03-15 15:46:39