0

我有一Product堂课:

class Product
{
    public string Name { get; set; }
    public DateTime ProductionDate { get; set; }
    public int CategoryId { get; set; }
}

如果我有一个List<Product>and 我想GroupBy通过ProductionDateand CategoryId。我愿意:

List<Product> products = GetAllProducts();

var groupedProducts = products.Groupby(p => new { p.ProductionDate, p.CategoryId })
                              .Select(pgroup => new {
                                                  prodDate = pgroup.Key.ProductionDate.ToString(),
                                                  categoryId = pgroup.Key.CategoryId.ToString(),
                                                  amount = pgroup.Count() 
                                                });

对于每一个categoryId,我都想知道products在特定的prodDate. 所以我创建了一个Dictionary对象,对于每个categoryId键,我存储prodDateamount。对象是一个Dictionary<string, List<Data>>with

class Data
{
     public string xValue { get; set; }
     public string yValue { get; set; }
}

为此,我尝试了:

Dictionary<string, List<Data>> productsPerPeriodPerCategory = new Dictionary<string, List<Data>>();
productsPerPeriodPerCategory = groupedProducts
                               .ToDictionary(p => p.categoryId, 
                                             p => new List<Data>().AddRange(
                                                  groupedProducts.Where(g => g.categoryId == p.categoryId)
                                                                 .Select(x => new Data()
                                                                    {
                                                                       xValue = x.prodDate,
                                                                       yValue = x.amount.ToString()
                                                                    }).ToList()));

但它给了我以下错误:

Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer<string>' because it is not a delegate type
4

2 回答 2

2

我认为问题在于AddRange不返回列表,并且您已经分组,因此不需要Where.

这应该足够了:

productsPerPeriodPerCategory = groupedProducts
           .ToDictionary(p => p.categoryId, 
                         p => p.Select(x =>  new Data()
                                             {
                                                 xValue = x.prodDate,
                                                 yValue = x.amount.ToString()
                                             }).ToList());      

如果您不需要中间组,您也可以在 1 步中完成所有操作

List<Product> products = GetAllProducts();
var groupedProducts = products.Groupby(p => new { p.ProductionDate, p.CategoryId })  
                          .ToDictionary(
                               x => x.Key.CategoryId,
                               x => new Data()
                                     { 
                                         xValue = x.Key.ProductionDate.ToString(),
                                         yValue = x.Count().ToString()
                                     }); 
于 2012-12-06T13:45:48.203 回答
1

试试这个:

productsPerPeriodPerCategory =
        groupedProducts
            .GroupBy(p => p.categoryId)
            .ToDictionary(
                g => g.Key,
                g =>
                g.Select(
                    r =>
                    new Data {xValue = r.prodDate, yValue = r.amount.ToString()}));
于 2012-12-06T13:52:06.143 回答