C# / LINQ

LINQ Filtering Operators with Examples in C#

Filtering operators are used to select only those elements from sequence that satisfy a condition. For example, suppose we have ten names in a collection sequence and we have to filter out those names that start with “K”.

Filtering OperatorsDescription
WhereFilter elements based on the condition.
OfTypeThis operator takes a Type name and select only those elements in the collection that have matching type.For example, we have a collection of base class (List<BaseClass>) and we have added some items of derived class in the collection. Now we have to find out those item that have of type derived type OfType<DerivedClass>.

C# Example of Where LINQ Operator

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

namespace ConsoleApplication1
{
    internal class Course
    {
        public int ID { get; set; }
        public string Subject { get; set; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            List<Course> courses = new List<Course>();
            courses.Add(new Course
            {
                ID = 1,
                Subject = "LINQ Tutorials"
            });

            courses.Add(new Course
            {
                ID = 2,
                Subject = ".NET Threading Tutorials"
            });

            courses.Add(new Course
            {
                ID = 3,
                Subject = "Learn WPF"
            });

            var result = courses.Where(w => w.Subject.Contains("Tutorials")).Select(w => w.Subject);

            foreach (var subject in result)
            {
                Console.WriteLine(subject);
            }
        }
    }
}

Results
==========
LINQ Tutorials
.NET Threading Tutorials

C# Example of OfType LINQ Operator

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

namespace ConsoleApplication1
{
    internal class Course
    {
        public int ID { get; set; }
        public string Subject { get; set; }
    }

    internal class PrimaryCourse : Course
    {
        public int PrimaryCourseFees { get; set; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            List<Course> courses = new List<Course>();
            courses.Add(new Course
                {
                    ID = 1,
                    Subject = "LINQ Tutorials"
                });

            courses.Add(new Course
                {
                    ID = 2,
                    Subject = ".NET Threading Tutorials"
                });

            courses.Add(new Course
            {
                ID = 3,
                Subject = "Learn WPF"
            });

            courses.Add(new PrimaryCourse
            {
                ID = 4,
                Subject = "Rhino Mocks tutorials",
                PrimaryCourseFees = 55
            });

            var result = courses.OfType<PrimaryCourse>().Select(w => w.Subject);

            foreach (var subject in result)
            {
                Console.WriteLine(subject);
            }
        }
    }
}

Results
========
Rhino Mock Tutorials