Query Composition Examples

This post and the next one I’ll be demonstrating how you can use helper methods with queries in them. Then how you can call on them in another query in a seperate class. This example will show Query or Comprehension syntax.

QueryCompSolution QueryCompProgram

 

EmployeeRepo.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLinq
{
    public class EmployeeRepo
    {
        // Public Methods
        //
        
        // Method that returns the private employee list collection.
        public IEnumerable<Employee> GetAll()
        {
            return _employees;
        }

        // Method that executes a query to return all the employees of a department by the department ID.
        //
        public IEnumerable<Employee> GetEmployeesInDepartmentById(int departmentId)
        {
            return
                from employee in _employees
                where employee.DepartmentID == departmentId
                select employee;
        }

        // Private Methods
        //

        // Creating a simple in memory list of employees.
        List<Employee> _employees = new List<Employee>
        {
            new Employee(1, "Daniel", 1),
            new Employee(2, "Sara", 2),
            new Employee(3, "John", 4),
            new Employee(4, "Debbie", 2),
            new Employee(5, "Poonam", 2),
            new Employee(6, "Jack", 3),
            new Employee(7, "Ben", 4)
        };
    }
}

The first thing you should notice is the addition of another method in this class. The GetEmployeesByDepartmentID method. What this method does is take an integer used for the department ID and uses it to locate all the employees in a department by that integer. The method returns the results of a comprehension query.

 
Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLinq
{
    class Program
    {
        /// <summary>
        /// Demonstrating more queries and how the execution can be deferred even when chaining queries from multiple classes.
        /// I'll be using comprehension queries for these demonstrations.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            executeSimpleQueryComposition();
            executeExtendedQueryComposition();

            Console.ReadLine();
        }

        // Private Methods
        //
        private static void executeSimpleQueryComposition()
        {
            // Demonstrating a way to get all the employees of a department by the department ID by calling on
            // the GetEmployeesInDepartmentById method in the EmployeeRepo class.
            var employeeRepo = new EmployeeRepo();
            var query = 
                from employee in employeeRepo.GetEmployeesInDepartmentById(2)
                select employee;

            displayResultHeader("****Displaying A Results Of A Simple Execution Query****");
            write(query);
        }

        private static void executeExtendedQueryComposition()
        {
            // Demonstrating a way to extend the query once the GetEmployeesInDepartmentId method is called on.
            var employeeRepo = new EmployeeRepo();
            var query =
                from employee in employeeRepo.GetEmployeesInDepartmentById(2)
                where employee.Name.Length < 6
                select employee;

            displayResultHeader("****Displaying Results Of A Extended Query****");
            write(query);
        }

        private static void displayResultHeader(string displayText)
        {
            Console.WriteLine(displayText);
        }

        private static void write(IEnumerable<Employee> query)
        {
            foreach (var item in query)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine();
        }
    }
}

I’ve created two methods as examples with different comprehension queries. I’m basically trying to find all the departments by a particular department ID in one method. The other method takes that same query and further defines it by employees in a particular department ID AND whose names are greater than 6 characters. One interesting thing about these queries is how they are chained when I call the helper method in the EmployeeRepo class. This is an example of deferred execution. Meaning the code isn’t run at compile time. It’s executed at run time when the query is requested. There are pro’s and con’s to this approach depending on how you do things. I actually like it because I can save computing resources with these queries.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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