【发布时间】:2012-01-06 15:50:29
【问题描述】:
我正试图围绕一个 Linq 查询,仅当所有子对象都包含特定属性时才返回父对象。
例如,返回类别,其中链接到该类别的所有产品都有product.Inventory == 0
此问题的其他标题:
选择所有 ChildObject 都具有特定 ChildObject.Parameter 值的 ParentObject
编辑:
除了关系之外,我还只想获取一个类别,如果它的日期属性之一不为空。
编辑:
这是我之前尝试过的示例之一:
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,否则我会得到没有事务的记录(我认为.All 会被过滤掉)。
【问题讨论】:
-
这是我之前尝试过的一个例子。你能告诉我这是否应该工作吗?
codevar selectQuery = (from statementDetail in pcardDatabaseContext.PCardStatementDetails where statementDetail.ExportedDate != null && statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved) orderby statementDetail.ExportedDate select statementDetail) as IOrderedQueryable; code
标签: c# linq entity-framework entity-relationship iqueryable