Version

Searching in Nodes

This topic demonstrates how to implement search in an organization chart.

The topic is organized as follows:

Introduction

A node of the xamOrgChart™ control is included in the search results if its underlying data fulfills certain conditions. The conditions are specified using a method delegate or a lambda expression. The search results are represented as a collection of OrgChartNode objects.

There are two ways to implement searching in nodes:

  • using a method delegate

  • using a Lambda Expression

The rest of the topic deals with them in detail.

Implementing Searching in Nodes

Implementing Searching in Nodes Using a Method Delegate

When using a method delegate, you define the delegate first and then use it to perform the search.

  1. Define a search condition matching the ConditionMethod delegate:

In C#:

bool SearchEmployee(object data)
{
    if(data is Employee)
    {
        Employee employee = (Employee)data;
        if(employee.FirstName == "John")
        {
            return true;
        }
    }
    return false;
}

In Visual Basic:

Function SearchEmployee(data As Object) As Boolean
    If TypeOf data Is Employee Then
        Dim employee As Employee = DirectCast(data, Employee)
        If employee.FirstName = "John" Then
            Return True
        End If
    End If
    Return False
End Function
  1. Perform the search.

In C#:

IEnumerable<OrgChartNode> searchResults = orgChartInstance.Search(SearchEmployee);

In Visual Basic:

Dim searchResults As IEnumerable(Of OrgChartNode) = orgChartInstance.Search(AddressOf SearchEmployee)

Implementing Searching in Nodes Using a Lambda Expression

When using a lambda expression, the search condition is defined directly in the Search method.

In C#:

IEnumerable<OrgChartNode> searchResults = orgChartInstance.Search(data =>
{
    if (data is Employee)
    {
        Employee employee = (Employee)data;
        if (employee.FirstName == "John")
        {
            return true;
        }
    }
    return false;
});

In Visual Basic:

Dim searchResults As IEnumerable(Of OrgChartNode) = orgChartInstance.Search(Function(data)
    If TypeOf data Is Employee Then
        Dim employee As Employee = DirectCast(data, Employee)
        If employee.FirstName = "John" Then
            Return True
        End If
    End If
    Return False
    End Function)