如何按最近2个月对列表进行分组,并在C#中计算其不同的项目

时间:2017-03-05 16:18:06

标签: c# linq

美好的一天。我想在最近2个月内对列表进行分组,并计算每个月的产品数量,并计算每月销售的每种产品的不同类别

我有一份清单。它包含一个名为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"});

我想要获得如下数据。请参阅附件图片

Month    --        productCount     --       categoryCount

March    --               2         --              1

February --               3         --              2

2 个答案:

答案 0 :(得分:1)

首先,您可以通过从当前日期减去当前日期来计算您想要的最早日期,以获得当月的开头,然后减去1个月以获得上个月的开始日期。然后你用它来过滤你的收藏,然后在月份上分组,并选择产品和类别的不同数量。

DateTime beginningOfPreviousMonth = DateTime.Today
    .AddDays(-DateTime.Today.Day)
    .AddMonths(-1);
var results = productList.Where(p => p.date_sold.Date >= beginningOfPreviousMonth)
    .GroupBy(p => p.date_sold.Month)
    .Select(grp => new 
    {
        Month = new DateTime(1,key,1).ToString("MMMM"),
        ProductCount = grp.Select(x => x.product).Distinct().Count(),
        CategoryCount = grp.Select(x => x.category).Distinct().Count()
    });

foreach(var result in results)
    Console.WriteLine(result);

答案 1 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ProductTransaction.productList = new List<ProductTransaction>() {
               new ProductTransaction () {product="sword", date_sold= DateTime.Parse("03/05/2017"),category="weapons"},
               new ProductTransaction() {product="sword2", date_sold=DateTime.Parse("03/01/2017"),category="weapons"},
               new ProductTransaction() {product="potion", date_sold=DateTime.Parse("02/05/2017"),category="life"},
               new ProductTransaction() {product="jacket", date_sold=DateTime.Parse("02/03/2017"),category="clothing"},
               new ProductTransaction() {product="jacketofBear", date_sold=DateTime.Parse("02/01/2017"),category="clothing"}
            };

            var results = ProductTransaction.productList
                .Where(x => (x.date_sold >= DateTime.Parse("1/1/2017")) && (x.date_sold <= DateTime.Parse("3/31/2017")))
                .GroupBy(x => x.date_sold.Month)
                .Select(x => new
                {
                    month = x.Key,
                    year = x.FirstOrDefault().date_sold.Year,
                    category_count = x.GroupBy(y => y.category).Count(),
                    product_count = x.Count()
                }).ToList();
        }
    }

    class ProductTransaction
    {
        public static List<ProductTransaction> productList = new List<ProductTransaction>();

        public string product;
        public DateTime date_sold;
        public string category;
    }
}