Queries Using TypeOf

This is something I’ve run across now and then. In some cases you may find someone who decides to stick a bunch of different types in a collection. Now I’m a big proponent of using strongly types object, collections, etc… However I know not everyone does that. Or there might be a situation where it didn’t happen that way. Here’s one of many ways that you can use a LINQ query to extract different kinds of data from an abstract collection.

Continue reading

Advertisements

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. 🙂

Joins Examples

The LINQ keyword Joins. This keyword gives you the ability to join data properties, database tables, etc.., that don’t have any kind of association. For example say you have tables with ID properties in two database tables without any kind of association from a database perspective. What the join keyword can do is join the two ID properties of the tables together to run queries on.

There are three types of Joins I’ll be showing here. Normal Joins, group joins, and cross joins. All of them will be using query syntax. I’ll be demonstrating method syntax in some later examples.

Again if you’re confused about the terminology of the different query types use this. Query = Comprehension Syntax, Lambda = Method Syntax.

Continue reading