Anonymous Type Conversions

LINQ query anonymous type conversions can get a bit tricky at times. I’ve decided to tackle a few ways in this example. I’ll be showing you a couple demonstrations on in-memory objects and getting data from a database. With these two ways I’ll be adding the converted objects to Lists<T> and Dictionary<T> collections. I’m working on a couple of other scenarios to accomplish this. However this example is what I’ve come up with so far. So in this case this solution is getting posted..:)

Continue reading

More Examples Of Join Operations

I went a little nuts on this simple console program. I wanted to demonstrate different ways to use the Join operator with in-memory objects from a collection and pulling information from a database. In the process of wanting to leverage anonymous types in the query I was playing around with different ways to display the data. Do I just iterate thru the query and display it. Or do I take that data, create objects, and populate another collection type with the data. Again, your personal preference might be different. I was just playing around with what ways would work.

I broke the code up into different repositories for the in-memory data and database data. I also created a WriteResults method in an attempt to use a central class to display data. I know lots of work for a simple console program. However I think it shows some cool core concepts with LINQ queries.

You will notice that the one common class that’s the same from other examples is the Employee and Department classes along with their respective repository classes. I’ve added a couple more for the database data which I’ll go over below.

Continue reading

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

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.:)