Click or drag to resize

XEnumerableToHierarchyTSource, TId Method

Builds a hierarchy from a flat sequence of elements, based on an Id/ParentId relation.

Namespace:  Linq.Extras
Assembly:  Linq.Extras (in Linq.Extras.dll) Version: 5.0.0+96a4f4bfed64095342c5df107c1fe1fb95603ee5
Syntax
public static IEnumerable<INode<TSource>> ToHierarchy<TSource, TId>(
	this IEnumerable<TSource> source,
	Func<TSource, TId> idSelector,
	Func<TSource, TId> parentIdSelector,
	TId rootParentId = null
)

Parameters

source
Type: System.Collections.GenericIEnumerableTSource
The flat sequence of elements to build a hierarchy from.
idSelector
Type: SystemFuncTSource, TId
A function that returns the id of an element.
parentIdSelector
Type: SystemFuncTSource, TId
A function that returns the parent id of an element.
rootParentId (Optional)
Type: TId
The parent id of the root elements (0 or -1, for instance).

Type Parameters

TSource
The type of the elements in source.
TId
The type of the elements' identifier.

Return Value

Type: IEnumerableINodeTSource
A sequence containing the root nodes of the hierarchy. Each node in the hierarchy has a collection of child nodes and a link to the parent node.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
See Also