I need to group it based on month and get the Max and Min counts.
Hear, Sep Month has Max count and Jun Month has Min count. So I need max value as 5 and Min value as 1.
Thanks in advance.
public class Test
{
public string Month { get; set; }
public string ID { get; set; }
public string SubMonth { get; set; }
}
List<Test> test = new List<Test>();
test.Add(new Test { Month = "Sep", ID ="1", SubMonth = "Sep2" });
test.Add(new Test { Month = "Sep", ID = "1", SubMonth = "Sep3" });
test.Add(new Test { Month = "Sep", ID = "1", SubMonth = "Sep4" });
test.Add(new Test { Month = "Sep", ID = "1", SubMonth = "Sep5" });
test.Add(new Test { Month = "Sep", ID = "1", SubMonth = "Sep6" });
test.Add(new Test { Month = "Jun", ID = "3", SubMonth = "Jun2" });
test.Add(new Test { Month = "Jul", ID = "4", SubMonth = "Jul2" });
test.Add(new Test { Month = "Jul", ID = "4", SubMonth = "Jul3" });
test.Add(new Test { Month = "Jan", ID = "5", SubMonth = "Jan2" });
test.Add(new Test { Month = "Jan", ID = "5", SubMonth = "Jan3" });
test.Add(new Test { Month = "Jan", ID = "5", SubMonth = "Jan4" });
test.Add(new Test { Month = "Jan", ID = "5", SubMonth = "Jan5" });
3
Answers
Using
linq
you can do it in this way.You can do in this way. You will get max and min count:
If you just want the minimum and maximum counts, you can use
Aggregate
with a tuple to collect them:If you also want to know the minimum and maximum Month, you need to save that in the tuple as well: