skip to Main Content

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


  1. Using linq you can do it in this way.

    var sepCount = test.Where(x => x.Month == "Sep").Count();
    var junCount = test.Where(x => x.Month == "Jun").Count();
    // you can get the count of each month in the list
    var monthCount = test.GroupBy(x => x.Month).ToList().Select(x => x.Count()).ToList();
    
    Login or Signup to reply.
  2. You can do in this way. You will get max and min count:

    var result = (test.GroupBy(item => item.Month)
                            .Select(itemGroup => new { Item = itemGroup.Key, Count = itemGroup.Count() })
                            .OrderByDescending(Item => Item.Count).ThenBy(Item => Item.Item)
                         ).ToList(); 
            Console.WriteLine("{0} {1}rn{2} {3}", result.First().Item, result.First().Count, result.Last().Item, result.Last().Count);
    
    Login or Signup to reply.
  3. If you just want the minimum and maximum counts, you can use Aggregate with a tuple to collect them:

    var minmax = test.GroupBy(t => t.Month)
                     .Select(tg => new { Month = tg.Key, Count = tg.Count() })
                     .Aggregate((Min: int.MaxValue, Max: int.MinValue), (mm, mc) => ((mc.Count < mm.Min ? mc.Count : mm.Min), (mc.Count > mm.Max ? mc.Count : mm.Max)));
    

    If you also want to know the minimum and maximum Month, you need to save that in the tuple as well:

    var minmaxMonth = test.GroupBy(t => t.Month)
                          .Select(tg => new { Month = tg.Key, Count = tg.Count() })
                          .Aggregate((Min: new { Month = "Min", Count = int.MaxValue }, Max: new { Month = "Max", Count = int.MinValue }),
                                     (mm, mc) => ((mc.Count < mm.Min.Count ? mc : mm.Min), (mc.Count > mm.Max.Count ? mc : mm.Max)));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search