What Was I Thinking?

Follies & Foils of .NET Development
posts - 77, comments - 203, trackbacks - 0

My Links

News

Archives

Post Categories

Check These Out

Gurus

Tuesday, March 29, 2011

QuickTip: Selecting Children from a Parent Item in Linq

I’m posting this here because I keep forgetting the syntax, and thought others might benefit as well.

 

Given :

public class ParentItem

{

   IEnumerable<ChildItem> Children

}

 

Selecting all the childitem instances from an IEnumerable<ParentItem>:

var allChildren = ParentItems.SelectMany(parent=>parent.Children);

 

Selecting matching childItem instances from an IEnumerable<ParentItem>:

var selectedChildren = ParentItems.SelectMany(parent=>parent.Children).Where(child=><boolean expression for filtering children>)

Posted On Tuesday, March 29, 2011 3:47 PM | Feedback (1) | Filed Under [ Visual Studio ]

Powered by: