【问题标题】:Distinct and group by at the same time in LINQ?在LINQ中同时区分和分组?
【发布时间】:2011-01-22 09:06:19
【问题描述】:

假设我有以下课程:

Product { ID, Name }

Meta { ID, Object, Key, Value }

Category { ID, Name }

Relation {ID, ChildID, ParentID } (Child = Product, Parent = Category)

还有一些示例数据:

产品:

ID   Name

1    Chair
2    Table

元

ID   Object  Key     Value

1      1     Color   "Red"
2      1     Size    "Large"
3      2     Color   "Blue"
4      2     Size    "Small"

类别

ID   Name

1    Indoor
2    Outdoor

关系

ID   ChildID   ParentID

1       1         1
2       1         2
3       2         1

我们可以使用 Distinct 和 Group by 来产生以下格式(ProductDetail)

ID=1,
Name=Chair,
Parent=
{
  { ID=1, Name="Indoor" },
  { ID=2, Name="Outdoor" }
},
Properties { Color="Red", Size="Large" }

ID=2,
Name=Table,
Parent=
{
  { ID=1, Name="Indoor"}
},
Properties { Color = "Blue", Size = "Small" }

我们可以通过使用得到第一项的“颜色”值

ProductDetails[0].Properties.Color

任何帮助将不胜感激!

【问题讨论】:

    标签: entity-framework c#-4.0 linq-to-entities group-by distinct


    【解决方案1】:

    不,您不能根据您所说的来执行此操作 - 因为“颜色”和“尺寸”是数据的一部分,而不是模型的一部分。它们仅在执行时才知道,因此除非您使用动态类型,否则您将无法通过 Properties.Color 访问它。

    不过,您也可以使用 Properties["Color"]:

    var query = from product in db.Products
                join meta in db.Meta 
                    on product.ID equals meta.Object 
                    into properties
                select new { Product = product,
                             Properties = properties.ToDictionary(m => m.Key,
                                                                  m => m.Value) };
    

    因此,对于每个产品,您都会有一个属性字典。这在逻辑上有效,但您可能需要对其进行调整以使其在实体框架中工作 - 我不知道它对 ToDictionary 的支持程度。

    编辑:好的,我将把上述内容作为“理想”的解决方案,但如果 EF 不支持 ToDictionary,您必须在进程中完成该部分:

    var query = (from product in db.Products
                 join meta in db.Meta 
                     on product.ID equals meta.Object 
                     into properties
                 select new { product, properties })
                .AsEnumerable()
                .Select(p => new {
                        Product = p.product,
                        Properties = p.properties.ToDictionary(m => m.Key,
                                                               m => m.Value) });
    

    【讨论】:

    • 感谢您的评论,但糟糕的 EF 不支持 ToDictionary 运算符 T T
    • @NVA:在这种情况下,您可能可以使用 AsEnumerable... 编辑来做到这一点。
    • 再次感谢您的快速回复,但是当我想使用连接从另一个表(在我的帖子中编辑)中包含一些额外信息时,生成的 SQL 太长了一堆 SELECT ... FROM ...有没有办法清理它?
    【解决方案2】:

    我在学习 LINQ 时遇到了这个问题,但我不满意 Jon 的输出与问题匹配(对不起 Jon)。以下代码返回与您的问题结构更匹配的匿名类型对象列表:

    var ProductDetails = (from p in Product
                          let Parents = from r in Relation
                                        where r.ChildID == p.ID
                                        join c in Category on r.ParentID equals c.ID
                                            into RelationCategory
                                        from rc in RelationCategory
                                        select new
                                        {
                                            rc.ID,
                                            rc.Name
                                        }
                          join m in Meta on p.ID equals m.Object into ProductMeta
    
                          select new
                          {
                              p.ID,
                              p.Name,
                              Parent = Parents.ToList(),
                              ProductMeta
                          })
                         .AsEnumerable()
                         .Select(p => new
                         {
                             p.ID,
                             p.Name,
                             p.Parent,
                             Properties = p.ProductMeta
                                           .ToDictionary(e => e.Key, e => e.Value)
                         }).ToList();
    

    这主要归功于 Jon Skeet 和 Visual Studio 调试器;)

    我知道您现在可能已经继续前进,但希望这可能会帮助其他想要学习 LINQ 的人,就像我一样。

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 2011-10-22
      • 2016-04-03
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多