Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何转换列表<;T>;输入字典<&燃气轮机;使用T属性作为键,其余属性作为列表<;T1>;_C#_Linq_List_Properties_Dictionary - Fatal编程技术网

C# 如何转换列表<;T>;输入字典<&燃气轮机;使用T属性作为键,其余属性作为列表<;T1>;

C# 如何转换列表<;T>;输入字典<&燃气轮机;使用T属性作为键,其余属性作为列表<;T1>;,c#,linq,list,properties,dictionary,C#,Linq,List,Properties,Dictionary,我有一个产品课程: class Product { public string Name { get; set; } public DateTime ProductionDate { get; set; } public int CategoryId { get; set; } } 如果我有一个列表,我想按ProductionDate和CategoryId分组。我有: List<Product> products = GetAllProducts(); v

我有一个
产品
课程:

class Product
{
    public string Name { get; set; }
    public DateTime ProductionDate { get; set; }
    public int CategoryId { get; set; }
}
如果我有一个
列表
,我想按
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() 
                                                });
为此,我尝试:

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()));
字典产品perperperiodpercategory=新字典();
ProductsPerPerPerPeriodPercategory=分组产品
.ToDictionary(p=>p.categoryId,
p=>new List().AddRange(
groupedProducts.Where(g=>g.categoryId==p.categoryId)
.选择(x=>新数据()
{
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
无法将lambda表达式转换为类型“System.Collections.Generic.IEqualityComparer”,因为它不是委托类型

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

这应足以:

productsPerPeriodPerCategory = groupedProducts
           .ToDictionary(p => p.categoryId, 
                         p => p.Select(x =>  new Data()
                                             {
                                                 xValue = x.prodDate,
                                                 yValue = x.amount.ToString()
                                             }).ToList());      
如果你不需要中间组,你也可以一步完成这一切

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()
                                     }); 
List products=GetAllProducts();
var groupedProducts=products.Groupby(p=>new{p.ProductionDate,p.CategoryId})
.ToDictionary(
x=>x.Key.CategoryId,
x=>新数据()
{ 
xValue=x.Key.ProductionDate.ToString(),
yValue=x.Count().ToString()
}); 

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

这应足以:

productsPerPeriodPerCategory = groupedProducts
           .ToDictionary(p => p.categoryId, 
                         p => p.Select(x =>  new Data()
                                             {
                                                 xValue = x.prodDate,
                                                 yValue = x.amount.ToString()
                                             }).ToList());      
如果你不需要中间组,你也可以一步完成这一切

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()
                                     }); 
List products=GetAllProducts();
var groupedProducts=products.Groupby(p=>new{p.ProductionDate,p.CategoryId})
.ToDictionary(
x=>x.Key.CategoryId,
x=>新数据()
{ 
xValue=x.Key.ProductionDate.ToString(),
yValue=x.Count().ToString()
}); 
试试这个:

productsPerPeriodPerCategory =
        groupedProducts
            .GroupBy(p => p.categoryId)
            .ToDictionary(
                g => g.Key,
                g =>
                g.Select(
                    r =>
                    new Data {xValue = r.prodDate, yValue = r.amount.ToString()}));
试试这个:

productsPerPeriodPerCategory =
        groupedProducts
            .GroupBy(p => p.categoryId)
            .ToDictionary(
                g => g.Key,
                g =>
                g.Select(
                    r =>
                    new Data {xValue = r.prodDate, yValue = r.amount.ToString()}));

两个问题。1) 哪些表格
groupedProducts
?如果您的
GroupBy
是正确的,则不需要
。其中(g=>g.categoryId==p.categoryId)
。2) 为什么要对项目调用
ToList()
,然后将该列表添加到新列表中。这似乎是多余的。1)g.categoryId==p.categoryId,如果您看到它们位于不同的作用域级别。p、 categoryId位于更高的范围内,它保持固定,而g.categoryId在集合上“循环”。GroupBy由多个列组成,因此给定一个固定的prodDate,可以有不同的类别ID 2)是的,你是对的,但是如果我删除它,我不会解决很多两个问题。1) 哪些表格
groupedProducts
?如果您的
GroupBy
是正确的,则不需要
。其中(g=>g.categoryId==p.categoryId)
。2) 为什么要对项目调用
ToList()
,然后将该列表添加到新列表中。这似乎是多余的。1)g.categoryId==p.categoryId,如果您看到它们位于不同的作用域级别。p、 categoryId位于更高的范围内,它保持固定,而g.categoryId在集合上“循环”。GroupBy是在多个列上生成的,因此给定一个固定的prodDate,可以有不同的类别ID 2)是的,你是对的,但是如果我删除它,我不会解决很多问题!但是“AnonymousType#1”不包含“Select”的定义,并且找不到接受“AnonymousType#1”类型的第一个参数的扩展方法“Select”(是否缺少using指令或程序集引用?)。在p.选择您的第一个code@Jamiec您可以在
ToDictionary中获得
ArgumentException“字典中已存在具有相同键的元素”
(x=>x.Key.CategoryId,…
谢谢!但是'AnonymousType#1'不包含'Select'的定义,并且找不到接受'AnonymousType#1'类型的第一个参数的扩展方法'Select'(是否缺少using指令或程序集引用?)code@Jamiec您可以获得
ArgumentException“字典中已存在具有相同键的元素”
ToDictionary中(x=>x.Key.CategoryId,…
您的解决方案有效,谢谢。但是,您可以向我解释Select下的推理吗?它会获取所有具有groupby相同密钥的记录吗?您的解决方案有效,谢谢。但是,您可以向我解释Select下的推理吗?它会获取具有groupby相同密钥的所有记录吗?