Home > Development > Linq Where Operator

Linq Where Operator


using System.Collections.Generic;
using System.Linq;

namespace LinqSnippets
{

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class Program
    {
        static void Main()
        {
            List<Student> students = new List<Student>
                                         {
                                             new Student
                                                 {
                                                     Id = 1,
                                                     Name = "Blair",
                                                     Age = 29
                                                 },
                                                 new Student
                                                 {
                                                     Id = 2,
                                                     Name = "Tim",
                                                     Age = 56
                                                 },
                                                 new Student
                                                 {
                                                     Id = 3,
                                                     Name = "Tom",
                                                     Age = 20
                                                 },
                                                 new Student
                                                 {
                                                     Id = 4,
                                                     Name = "Sophie",
                                                     Age = 24
                                                 },
                                                 new Student
                                                 {
                                                     Id = 5,
                                                     Name = "Esther",
                                                     Age = 29
                                                 },
                                         };

            var youngStudents = students.Where(x => x.Age < 25);

        }
    }
}

You can see only students less than 25 are selected. The Where operator is the same as WHERE in SQL. It takes a delegate so you can use anonymous delegates or Lambdas as I am here.

The next post will look at operators selecting a single item.

Blair.

Advertisements
Categories: Development Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: