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
Syntaxpublic static IEnumerable<INode<TSource>> ToHierarchy<TSource, TId>(
this IEnumerable<TSource> source,
Func<TSource, TId> idSelector,
Func<TSource, TId> parentIdSelector,
TId rootParentId = null
)
<ExtensionAttribute>
Public Shared Function ToHierarchy(Of TSource, TId) (
source As IEnumerable(Of TSource),
idSelector As Func(Of TSource, TId),
parentIdSelector As Func(Of TSource, TId),
Optional rootParentId As TId = Nothing
) As IEnumerable(Of INode(Of TSource))
[<ExtensionAttribute>]
static member ToHierarchy :
source : IEnumerable<'TSource> *
idSelector : Func<'TSource, 'TId> *
parentIdSelector : Func<'TSource, 'TId> *
?rootParentId : 'TId
(* Defaults:
let _rootParentId = defaultArg rootParentId null
*)
-> IEnumerable<INode<'TSource>>
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:
IEnumerableINodeTSourceA 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