2
Answers

Group list by last 2 months and count its distinct items

Lance Kidd

Lance Kidd

7y
368
1
Hi to all. Good day. I want to group a list by last 2 months and count how many products are their in every month and also count distinct category for every product sold in each month



I have a list. It contains a class named ProductTransaction.
 
  • List<ProductTransaction> productList = new List<ProductTransaction>();
  • class ProductTransaction
  • {
  • string product;
  • DateTime date_sold;
  • string category;
  • }
    • productList.Add(new ProductTransaction () {product="sword", date_sold=03/05/2017,category="weapons"});
    • productList.Add(new ProductTransaction() {product="sword2", date_sold=03/01/2017,category="weapons"});
    • productList.Add(new ProductTransaction() {product="potion", date_sold=02/05/2017,category="life"});
    • productList.Add(new ProductTransaction() {product="jacket", date_sold=02/03/2017,category="clothing"});
    • productList.Add(new ProductTransaction() {product="jacketofBear", date_sold=02/01/2017,category="clothing"});



I want to achieve a data like below.Please see attached image file

Month          productCount     categoryCount

March                2                           1
February           3                           2


Thank you and GodBless
 
 
Answers (2)