I have a List<ShipmentInformation>
public class ShipmentInformation
{
public string Type { get; set; }
public long StartID { get; set; }
public long EndID { get; set; }
public DateTime BoxDate { get; set; }
}
I currently have this code to determine where the most stock is:
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;
However if the totals are exactly the same it will select the last item in TypeTotals so I now want to add a check in to make sure that the earliest BoxDate is used.
So lets say I have 10 items of Type A and 10 items of Type B, at the moment Type B will be chosen.
I want to now make sure that when I return the LargestType that it returns the earliest item with that type. So if any of my items in A have a BoxDate earlier than any of the items in B then A should be chosen.
Just save the minimum date for each type total and then take it into account in your aggregation (which by the way would be cleaner with a simple
foreachloop in my opinion)