Lambda Composition Examples

LambdaCompSolution LambdaCompProgram

 

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 LINQ lambda queries for these demonstrations.
        /// </summary>

        /// <param name="args"></param>
        static void Main(string[] args)
        {
            executeLambdaQuery();
            executeLambdaExtendedQuery();

            Console.ReadLine();
        }

        // Private Methods
        //
        private static void executeLambdaQuery()
        {
            // Demonstrating the use of a method call to a lambda query that gets all the employees of a department by the department ID.
            // The lambda query makes it so that call can be pretty clean, short, and simple.
            displayResultHeader("****Displaying Simple Lambda Query Results****");
            var employeeRepo = new EmployeeRepo();
            write(employeeRepo.LambdaGetEmployeesByDeptId(2));
        }

        private static void executeLambdaExtendedQuery()
        {
            // Demonstrating the use of a method call to a lambda query that gets all the employees of a department by the department ID
            // and continuing the filter with another lambda query by the employee name length.
            // This shows that deferred execution works with a lambda query.
            displayResultHeader("****Displaying Extended Lambda Query Results****");
            var employeeRepo = new EmployeeRepo();
            write(employeeRepo.LambdaGetEmployeesByDeptId(2).Where(e => e.Name.Length < 6));
        }

        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();
        }
    }
}

This example is using the same code from the repositories as the Query Composition post. The only difference is I’m using Lambda queries instead of composition queries to look up all the employees by a particular department. The other method further looks for all employees in a department and whose names are greater than six characters.

These two posts are to show you how both query types can be used. Again this all depends on your personal coding style or requirements. 🙂

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s