【发布时间】: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 })吗? - 我不知道提供商将如何翻译。