[英]Filter Parent Collection Using Criteria in Child Collection via a Linq Query
我有以下實體圖:
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>)
我想您可能正在尋找這樣的東西?
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
}
這個?
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))
});
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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.