Time for action – learn how to go about creating a Where() clause

Suppose there is a Student class and we want to find only those students who have enrolled for the C# course:

  1. We want to find all students whose Course is C#. Write the Lambda expression for that:
    s => s.Course.Equals("C#")
  2. Pass this Lambda expression to the Where() clause (sometimes, LSQO are referred to as clauses) as follows:
    Students.Where(s => s.Course.Equals("C#") );
  3. Receive the result of this operation in an implicit variable, as shown next:
    var cSharpStudents = Students.Where(s => s.Course.Equals("C#") );  

What just happened?

We have a List of Student objects and we wanted to find all the students that have opted for C#. Assuming Course is a public property of the Student object, ...

Get .NET 4.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.