Three options to filter a list in C#

Three options to filter a list in C#

I will show here three different ways to filter text by some string criteria in C#.

We have a public class called "Student" which holds Students' names. 

No alt text provided for this image

Every new object of Students is inserted into a list called ListOfStudents as follows.

I also added ListBox control which is bound to ListOfStudents, in order to display the results.

static List<Students> ListOfStudents = new List<Students>();

Students newStudentObj = new Students(txtAddStudent.Text);

ListOfStudents.Add(newStudentObj);

ListBox control bound to ListOfStudents

Now, in order to search the name of the student according to some text entered by the user, I can use a simple "Filter" method.

var searchResult = Filter(ListOfStudents, txtSearch.Text);

This method take the Students list as a first argument and the text to search as a second argument. Simple.

The second option is to create a Filter method as an Extension Method. This will allow me to call the filter method like this:

var searchResult = ListOfStudents.Filter(txtSearch.Text);

In order to accomplish this, I need to create a new static class inside the same namespace, let's call it 'SearchStudentByName', inside this class I will create a static Filter method which takes my Students list as a first argument, and the text to search as a second argument. The first argument is preceded by 'this' keyword, in order to extend the functionality of the method, and from now on every single instance of List of Students can use it. 

In addition I added a foreach to loop my list of students and return the result. Here is the implementation:

No alt text provided for this image

I can take this one more step forward, and use the third option. I will use #LINQ to accomplish this. LINQ is a C# library that enables query capabilities directly into the C# language, and can be used by adding System.Linq namespace.

I will use the LINQ's special Where method to loop the list of Students using 'x' iterator, and return the results that contains the text I am looking for as a list. Simple yet powerful.

No alt text provided for this image


要查看或添加评论,请登录

Orsan Awawdi的更多文章

  • Docker Disk Space Management

    Docker Disk Space Management

    It is necessary to regularly check the disk space occupied by Docker to ensure efficient resource management and…

  • Code Review

    Code Review

    Code Review is a sensitive matter. It introduces the code you wrote to the eyes of another person, who has their own…

  • re.findall

    re.findall

    When we talk about finding recurrent text in a string, we think about regex (Regular Expressions). Regex has so many…

    1 条评论
  • AI generated code

    AI generated code

    Should we always listen to AI generated code? I asked BLACKBOX.AI to write me a simple code in #python.

  • Environment variables

    Environment variables

    Environment variables are variables that store data in your program but outside your code. For example, key and secret…

    1 条评论
  • Understanding type annotation in Python

    Understanding type annotation in Python

    Why do we need type hints in Python? We can annotate (comment) variables and functions with data types. Being a…

  • Nested Repeaters

    Nested Repeaters

    Let's take an example. We have Categories table in our DB, and each Category has multiple subcategories.

  • Process transcript with Python

    Process transcript with Python

    Let's see what is the most popular word from Donald Trump speech. Transcript of the speech can be found when googling…

  • Validation using Attributes in C#

    Validation using Attributes in C#

    Validating data entered by user can be done via multiple methods. Attributes is one powerful yet simple way to validate…

社区洞察

其他会员也浏览了