LINQ使用GroupBy的字典对象

时间:2022-01-05 20:55:01

I am use Dictionary object in my LINQ Group By Clause.

我在LINQ Group By Clause中使用Dictionary对象。

It works fine as long as i don't have duplicate Item Name in the result.

只要我在结果中没有重复的Item Name,它就可以正常工作。

However, it is failing when GroupBy query encounter with duplicate Item Name.

但是,当GroupBy查询遇到重复的项目名称时,它会失败。

I am not sure whether i am using Dictionary object in my GroupBy clause correctly or not? Please advise.

我不确定我是否正确使用了GroupBy子句中的Dictionary对象?请指教。

This is my model

这是我的模特

public class Item
{
    public string CustomerName { get; set; }
    public Dictionary<string, float?> ItemCost { get; set; }
    public float? Cost{ get; set; }
    public float? TotalCost{ get; set; }
}

Following is my selection query (LINQ) along with GroupBy

以下是我的选择查询(LINQ)以及GroupBy

        var result = myFilteredQuery
            .Select(x => new
            {
                ItemName = x.Item.Name,
                Cost= x.Item.Cost,
                CustomerName = x.Item.Customer.Name,

            }).ToList();

Following is my GroupBy query.

以下是我的GroupBy查询。

            return result .GroupBy(x => new { x.CustomerName})
          .Select(x => new CostByItem
          {
              CustomerName = x.Key.CustomerName,
              LineItem = x.ToDictionary(y => y.ItemName, y => y.Cost),
              TotalCost= x.Sum(y => y.Cost)
          }).ToList();

p.s. I am trying to send create data structure Group By Customer and than under each customer dictionary object Item Cost (Item Name, Item Cost) and total cost for that customer.

附:我正在尝试发送创建数据结构Group By Customer,而不是每个客户字典对象下的项目成本(项目名称,项目成本)和该客户的总成本。

1 个解决方案

#1


If I'm understanding correctly, you want to group the items by customer, and then group the items by name under that. You need to use GroupBy twice.

如果我理解正确,您希望按客户对项目进行分组,然后按名称对项目进行分组。您需要两次使用GroupBy。

result.GroupBy(x => x.CustomerName)
      .Select(x => new CostByItem
      {
          CustomerName = x.Key,
          LineItem = x.GroupBy(y => y.ItemName)
                      .ToDictionary(y => y.Key, y => y.Sum(z => z.Cost)),
          TotalCost = x.Sum(y => y.Cost)
      }).ToList();

Also note that there's no need to use x => new { x.CustomerName}. Grouping by the string value is sufficient - creating an anonymous type is unnecessary.

另请注意,不需要使用x => new {x.CustomerName}。按字符串值分组就足够了 - 不需要创建匿名类型。

#1


If I'm understanding correctly, you want to group the items by customer, and then group the items by name under that. You need to use GroupBy twice.

如果我理解正确,您希望按客户对项目进行分组,然后按名称对项目进行分组。您需要两次使用GroupBy。

result.GroupBy(x => x.CustomerName)
      .Select(x => new CostByItem
      {
          CustomerName = x.Key,
          LineItem = x.GroupBy(y => y.ItemName)
                      .ToDictionary(y => y.Key, y => y.Sum(z => z.Cost)),
          TotalCost = x.Sum(y => y.Cost)
      }).ToList();

Also note that there's no need to use x => new { x.CustomerName}. Grouping by the string value is sufficient - creating an anonymous type is unnecessary.

另请注意,不需要使用x => new {x.CustomerName}。按字符串值分组就足够了 - 不需要创建匿名类型。