Here’s the code I’m having trouble with. I’m using an EDMX modeled from an existing database.
// All orders completely shipped Grouped by RefId
var RefIdsWithAllShippedOrders = mydbcontext.OrderDetails
.Where(s => s.Application.CustomerID == "MSFT")
.GroupBy(o => o.RefId)
.Where(t => t.All(i => i.Status.Description.ToUpper() == "SHIPPED"))
.Select(g => g.Key);
// Iterate through the RefIds
foreach (var refid in RefIdsWithAllShippedOrders)
{
// Gather all the orders that have the same RefIds
var OrdersForThisRefid = (from o in mydbcontext.OrderDetails
where o.RefId == refid
select o).AsEnumerable();
//gather all the orders with at least one Canadian recipient
var orderswithcandianrecipients = from o in OrdersForThisRefId
where o.OrderRecipients.All( w=> w.Country.Trim().ToUpper() == "CANADA") // ****
select o;
// Print RefIds of the orders that have at least one Canadian recipient
foreach (var eachorder in orderswithcandianrecipients)
{
Console.WriteLine(eachorder.RefId);
}
}
Here’s the schema I have:
ORDERDETAILS
RefId OrderId (PK)
ABC001 00001
ABC001 00002
ABC001 00003
ABC002 00004
ABC002 12355
ORDER RECIPIENTS
PK OrderID (FK) NAME COUNTRY
1 00001 LINCOLN USA
2 00001 JEFFERSON USA
3 00001 WASHINGTON CANADA
4 00001 FRANKLIN USA
5 00002 GRANT USA
6 00002 WILSON USA
7 12355 FORD CANADA
8 12355 JOHNSON USA
The result I’m hoping to get is a var type that contains orders that have at least one Canadian recipient. In the above example, that would be Orders with OrderID = 00001 and 12355
The code, it seems to not respect the Where and All filter that I have marked with *. It returns all orders. Please help me understand what am I doing wrong. Thank you so much.
I think you want
AnynotAll. By usingAllyou are saying all recipients on the order must be Canadian.Anywill give you orders that have at least one Canadian recipient.Another warning about
All. It does not look for all items to pass the condition, it looks for the first item that fail the condition. Therefore, if you have zero items, you do not have any that fail the condition, andAllwill always returntrue