I would like to be able to query the parent objects and filter the contents of the child collection.
For example, I have a collection of OrderHeaders. I want to query this collection using LINQ to return all OrderHeaders, but I want some related OrderDetail strings to be included.
I prefer looking for a solution where I can do all this in a single LINQ statement.
This console application demonstrates this.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LINQ { class Program { static void Main(string[] args) { List<OrderHeader> orders = GetOrderHeaders(); var filteredOrders = from p in orders where p.Detail.Where(e => e.StockCode == "STK2").Count() > 0 select p; foreach (var order in filteredOrders) { Console.WriteLine("Account {0} ", order.AccountCode); foreach (var detail in order.Detail) { Console.WriteLine("StockCode {0}, Quantity {1}", detail.StockCode, detail.Quantity); } Console.WriteLine(); }
Thanks so much for any suggestions.
Floor
c # linq
P2l
source share