0

私はProductクラスを持っています:

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

私が持っていてList<Product>、私がしたい場合GroupByProductionDateCategoryId。そうです:

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かを知りたいです。そこで、キーごとにとを格納するオブジェクトを作成します。オブジェクトはproductsprodDateDictionarycategoryIdprodDateamountDictionary<string, List<Data>>

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 に答える