【问题标题】:Anonymous type select object and its children匿名类型选择对象及其子对象
【发布时间】:2016-07-22 14:13:47
【问题描述】:
Categoires
    .Where(c => c.ID == 18)
    .Include("Products.ProductPrices")
    .Select(c => new
                        {
                            ID = c.ID,
                            Name = c.Name, // other properties
                            ProductAndPrices = c.Products.ProductPrice,
                            // from above what I really want is Products + there prices
                        })

我在这里对上面的这段代码有一个概念问题,

ProductAndPrices = c.Products.ProductPrice,

我能做到,

ProductAndPrices = c.Products,

但不是这个,

ProductAndPrices = c.Products.ProductPrice,

问题

使用 linqpad,我可以看到我的查询正在运行,并且在 Incude("Products.ProductPrices") 之前拥有所有数据,但是当我尝试创建一个匿名对象和平面时,它不允许我这样做。

请注意产品可以有多种价格。

编辑

这对我有用,

ProductAndPrices = c.Products.SelectMany(p => p.ProductPrice),

但上面只给出了 ProductPrices 列表,它根本不包括 Products 列,

我真正想要的是产品列 + 它的子项(即价格)

ProductPrice 也是一个集合,它是一个产品的价格历史列表。

【问题讨论】:

  • 您需要编辑您的问题并显示您的课程:Product 课程和ProductPrice 课程和Categories 课程
  • 我猜Products 是具有ProductPrice 属性的Product 的集合。所以你可以通过c.Products.Select(p => p.ProductPrice) 来获取价格,但不清楚你想要ProductAndPrice 是什么。
  • @juharr 我只是添加了一点解释,请检查它是否有意义,它是父母和孩子之间的一对多关系...... ProductPrice 也是一个集合,它是价格历史列表对于产品
  • 换句话说,产品本身+它的孩子
  • @Mathematics 你试过这个c.Products.Select(p => new { Product = p, Prices = p.ProductPrice }) 吗? - 我不知道提供商将如何翻译。

标签: c# linq


【解决方案1】:

Include 在使用投影时总是会被忽略。但是您可以通过以下方式获取所需的数据:

Categories
    .Where(c => c.ID == 18)
    .Include("Products.ProductPrices")
    .Select(c => new
                {
                    ID = c.ID,
                    Name = c.Name, // other properties
                    Products = c.Products
                                .Select(p => new
                                {
                                    Product = p,
                                    // Or maybe various Product properties
                                    ProductPrices = p.ProductPrice
                                })
                })

【讨论】:

  • 我想要完全相同,但 Products 对象有 2 个属性,而我希望它具有产品属性 + 价格作为儿童
  • 使用此代码,您将看到产品确实填充了它们的ProductPrices 集合,因为 EF 执行 relationship fixup,即它按实体自动填充导航属性在上下文的缓存中可用。
  • 还有一件事:在这种情况下,您应该防止延迟加载,否则 EF 会在您处理它时尝试延迟加载 Products.ProductPrices,即使在释放上下文时也是如此。
【解决方案2】:

您可以像这样选择价格:

ProductAndPrices = c.Products.Select(p => new { Product = p, Prices = p.ProductPrice })

【讨论】:

  • 谢谢,但上面不会只给我 ProductPrices 而不是 Products + ProductPrices 吗?我确实在上面尝试过 SelectMany
  • @Mathematics 然后你需要编辑你的问题并显示你的课程:Product class 和 ProductPrice class
【解决方案3】:

类似的东西

Categories
    .Where(c => c.ID == 18)
    .Include("Products.ProductPrices").SelectMany(c=> p.Products.ProductPrices, (parent, child) => 
                                                                new { Product = parent, ProductAndPrices = child});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多