我有以下实体图:
Item
IList<Prices>
DateTime Opened
DateTime? Closed
Price
Name
DateTime Opened
DateTime? Closed
如何选择Closed
为空的所有项目以及只有Closed
为空的价格?
正如您所看到的,Item
和Price
具有有效期限,因此我只需选择有效的商品并使用有效价格。
我知道如何选择项目,但我不知道如何约束“嵌套”价格。
Items.Where(i => i.Closed == null).Where(i => i.Prices <need constraint prices>)
答案 0 :(得分:3)
我想你可能正在寻找这样的东西?
Items.Where(i => !i.Closed.HasValue && i.Prices.Any(p => !p.Closed.HasValue))
这只会选择非收盘价的商品。
如果你想真正获得每个项目的非收盘价格清单,我会切换到LINQ语法并执行以下操作:
from i in Items
where !i.Closed.HasValue
from p in i.Prices
where !p.Closed.HasValue
group p by i into itemPrices
select new {
Opened = i.Opened
Closed = i.Closed
Items = itemPrices
}
答案 1 :(得分:2)
此?
items
.Where(x => x.Closed == null)
.Select(x =>
new Item
{
Closed = x.Closed,
Opened = x.Opened,
Prices = new List<Price>(x.Prices.Where(p => p.Closed == null))
});
答案 2 :(得分:0)
from item in items
where item.Closed == null
let prices = from price in item.Prices
where price.Close == null
select price
select new
{
Item = item,
Prices = prices.ToArray()
}