私はProduct
クラスを持っています:
class Product
{
public string Name { get; set; }
public DateTime ProductionDate { get; set; }
public int CategoryId { get; set; }
}
私が持っていてList<Product>
、私がしたい場合GroupBy
はProductionDate
、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
prodDate
amount
Dictionary<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