I am trying to wrap my head around a Linq query to return a parent object only if all child objects contain a specific property.
I.e., as an example, return categories where all products linked to that category have product.Inventory == 0
Other title for this Q:
Select ParentObject where all ChildObjects have specific ChildObject.Parameter value
EDIT:
In addition to the relationship, I also only want to get a category if one of it’s date properties is not null.
EDIT:
Here is one of the samples I have previously attempted:
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>;
EDIT:
Found a solution for my problem, but can’t self-answer for another 7 hours.
I had partially been experiencing some issues on an earlier syntax, I assumed that when using x.All the values wouldn’t return any match if the set was empty.
Here is what solved it for me:
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>;
Please note that I had modified the ExportDate to only retrieve ExportedDate == NULL.
Also, I had to add a .Any, otherwise I was getting records that had no Transactions (where I thought the .All would filter out).
Found a solution for my problem, but can’t self-answer for another 7 hours.
I had partially been experiencing some issues on an earlier syntax, I assumed that when using
x.Allthe values wouldn’t return any match if the set was empty.Here is what solved it for me:
Please note that I had modified the
ExportDateto only retrieveExportedDate == NULL.Also, I had to add a
.Any, otherwise I was getting records that had no Transactions (where I thought the.Allwould filter out).