选择没有产品库存的类别
本文关键字:产品库 选择 | 更新日期: 2023-09-27 18:19:34
我正试图绕过Linq查询,只在所有子对象都包含特定属性的情况下返回父对象。
例如,退货类别中,与该类别链接的所有产品都具有product.Inventory == 0
此Q的其他标题:
选择ParentObject,其中所有ChildObject都有特定的ChildObject。参数值
编辑:
除了关系之外,我还只想得到一个类别,如果它的日期属性之一不为空。
编辑:
这是我之前尝试过的一个样本:
var selectQuery =
(from statementDetail in pcardDatabaseContext.PCardStatementDetails
where statementDetail.ExportedDate != null
&& statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
orderby statementDetail.ExportedDate
select statementDetail) as IOrderedQueryable<PCardStatementDetail>;
编辑:
找到了我的问题的解决方案,但在接下来的7个小时内无法自行回答。
我在早期的语法中遇到了一些问题,我认为在使用x.All
时,如果集合为空,则值不会返回任何匹配。
以下是为我解决问题的方法:
var selectQuery =
(from statementDetail in pcardDatabaseContext.PCardStatementDetails
where statementDetail.ExportedDate == null
&& statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
&& statementDetail.PCardTransactions.Any()
orderby statementDetail.ExportedDate
select statementDetail) as IOrderedQueryable<PCardStatementDetail>;
请注意,我已将ExportDate
修改为仅检索ExportedDate == NULL
。此外,我必须添加一个.Any
,否则我得到的记录没有Transactions(我认为.All
会过滤掉)。
var categoriesWithNoInventory =
Categories.Where(c => c.Products.All(p => p.Inventory == 0));
假设你的类看起来像这个
public class Category
{
public string Name { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public string Name { get; set; }
public int Inventory { get; set; }
}
那么这将起作用(AllCategories()返回IEnumerable<Category>
)
var categories = AllCategories().Where(c => c.Products.All(p => p.Inventory == 0));
我认为您必须执行两个查询?首先获取子产品,然后检查库存,或者我想你可以通过连接来完成,这里有一个连接示例:http://www.dotnetperls.com/join
找到了我的问题的解决方案,但在接下来的7个小时内无法自行回答。
我在早期的语法中遇到了一些问题,我认为在使用x.All
时,如果集合为空,则值不会返回任何匹配。
以下是为我解决问题的方法:
var selectQuery =
(from statementDetail in pcardDatabaseContext.PCardStatementDetails
where statementDetail.ExportedDate == null
&& statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
&& statementDetail.PCardTransactions.Any()
orderby statementDetail.ExportedDate
select statementDetail) as IOrderedQueryable<PCardStatementDetail>;
请注意,我已修改了ExportDate
以仅检索ExportedDate == NULL
。此外,我必须添加一个.Any
,否则我得到的记录没有Transactions(我认为.All
会过滤掉)。