Reputation: 183
I tried to find the answer for my problem, but I am stucked. I have collection of arrays and need to remove records where some attributes are same.
Example: I have attributes - BeginTime, EndTime, Date, Price.
07:00, 11:00, 2011-11-14, 90
08:00, 12:00, 2011-11-14, 110
07:00, 11:00, 2011-11-15, 120
07:00, 11:00, 2011-11-14, 50
And I want to remove records where BeginTime, EndTime and Date are same and Price is not lowest. In this example, removed record would be the first one.
Thanks for any help or suggestion
Upvotes: 4
Views: 348
Reputation: 113442
How about with LINQ:
IEnumerable<Bar> bars = ...
var lowestPriceBars = bars.GroupBy(bar => new { bar.BeginTime, bar.EndTime, bar.Date } )
.Select(g => g.OrderBy(bar => bar.Price).First())
.ToArray();
This works by grouping items with the same timestamps together and then producing, from each group, the item with the smallest price. Do note that if there are multiple items in a group with the same lowest price, the query will retain one of them arbitrarily.
Also note that with a MinBy
operator (such as one from more morelinq), you could make the Select
operation more efficient with:
.Select(g => g.MinBy(bar => bar.Price))
EDIT: If you want to keep all the items with the lowest price, you can do:
var lowestPriceBars = bars.GroupBy(bar => new { bar.BeginTime, bar.EndTime, bar.Date })
.SelectMany(timeGroup => timeGroup
.GroupBy(bar => bar.Price)
.OrderBy(priceGroup => priceGroup.Key)
.First())
.ToArray();
Upvotes: 6