Sometime back, in the DNF forum a question has been asked about implementing an search like Android one. This post is an attempt to provide an answer for the same by providing a demonstration of using the Expression-Bodied Property of C# 6.0
Introduction
Sometime back, in the DNF forum a question has been asked about implementing an search like Android one. This post is an attempt to provide an answer for the same by providing a demonstration of using the Expression-Bodied Property of C# 6.0
Using the code
Let us first create a Person class and expose a property say PersonName and populate some data to it using the Expression-Bodied Property of C# 6.0 as shown below
public class Person
{
//Kindly note that the PersonName has been populated by using expression-bodied property of C#6.0
public List<string> PersonName => new List<string>
{
"Rajlakshmi Biswas",
"Niladri Biswas",
"Arina Biswas",
"RNA Team",
"Mahesh Nagar",
"Manish Nagar",
"Shan Mani",
"Ghanshaym rama"
};
}
Kindly notice the way the string values has been set to the PersonName property using the Expression-Bodied Property(=>) of C# 6.0. It's the Lambda way.
IL DASM reveals the below, however

Person is an instance class and has a property name PersonName but it is really a getter method. So Expression-Bodied Properties are syntactic sugar for the getter methods.Let's look at the same from another perspective.

Though we have not explicitly invoked the Add() method of the collection, but the compiler did that implicitly.
Now it's time to invoke the PersonName property.Let's do in the below way
var personNames = new Person().PersonName;
Also let's have another look that the PersonName property is read-only

Now once we have the collection at our disposal, we can write the below lines of code
//get the Person names
var personNames = new Person().PersonName;
//set the search term
string toSearch = "Sh";
//set the delimeter
string[] stringSeparator = new string[] { " " };
The next objective is to loop through each item of the collection and split that item based on the delimiter provided. If in any of the delimited values Starts with the search criteria then we need to display the output as asked in the question.
So let us go ahead and implement the below piece of code
static void Main(string[] args)
{
//get the Person names
var personNames = new Person().PersonName;
//set the search term
string toSearch = "Sh";
//set the delimeter
string[] stringSeparator = new string[] { " " };
personNames
.ForEach(str => //loop each item in the collection
//notice that it is statement lambda
{
if (str .Split(stringSeparator, StringSplitOptions.None) //split the string into parts as per the delimeter
.Any(i => i.StartsWith(toSearch)) //if any of the test condition satisfies
) Console.WriteLine(str.Replace(toSearch, "<b>" + toSearch + "</b>")); //perfom the action
});
Console.ReadKey();
}
Notice the use of Any() Extension method. It returns a Boolean value if any element of the provided data source matches the test condition.The output is as under

Change: string toSearch = "Ma"; and the result

References
a)
Properties
b)
Enumerable.Any
Conclusion
So in this article, we learnt
- Use of Expression-Bodied Property
- What Expression-Bodied Property is at the compiler level
- How to use Any Extension Method for searching an element inside a collection
- Usage of Statement Lambda
Hope this will be helpful.Thanks for reading.Zipped file attached.