I have a class:
public class LevelInfo
{
public int Id { get; set; }
public string HexColor { get; set; }
public string Caption { get; set; }
public int MinPrice { get; set; }
}
I have a collection of LevelInfo. For example, we have the following data:
Id HexColor Caption MinPrice
1 color1 name1 10
2 color2 name2 20
3 color2 name3 10
4 color3 name4 10
I want to get new collection of LevelInfo grouped(not exactly, don’t know how to say properly) by HexColor.
For the data above I want to get the following collection that contain 3 records:
Id HexColor Caption MinPrice
1 color1 name1 10
3 color2 name3 10
4 color3 name3 10
We not select record with Id equals 2 because we have color1 (with Id =1) and it’s have min price.
How can I do this?
Thanks.
So it sounds like you want to group, order within the group by MinPrice, and then select the first element from each group, right? If that’s correct, you want:
Alternatively, if you use MoreLINQ you can avoid doing a complete sort by
MinPrice, just picking the value with the minimum price: