LINQ Query Syntax Example

This simple LINQ example shows how I’m using LINQ to access an in memory Employee object. I’m also showing how to see the object type in the event it’s needed.

ProgramOutput SolutionExplorer

 

Employee.cs Code Contents

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

namespace MyLinq
{
    public class Employee
    {
        // Public Properties
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime HireDate { get; set; }
    }
}

A very simple class used when creating Employee objects. Capturing the ID, Name, and HireData as a DateTime object type.

 

Program.cs Code Contents

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

namespace MyLinq
{
    class Program
    {
        /// <summary>
        /// Demonstrating a couple of different ways LINQ can be used to simplify the accessing
        /// of data in memory or object types.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            QueryEmployee();
            QueryTypes();

            Console.ReadLine();
        }

        //Private Methods
        //
        private static void QueryEmployee()
        {
            Console.WriteLine("****LINQ Execution On Object Data****");

            // Creating am IEnumerable list of Employee type that I can use LINQ on.
            IEnumerable<Employee> employees = new List<Employee>()
            {
                new Employee { ID = 1, Name = "Daniel", HireDate = new DateTime(2002, 3, 5) },
                new Employee { ID = 1, Name = "Sara", HireDate = new DateTime(2002, 10, 15) },
                new Employee { ID = 1, Name = "Jack", HireDate = new DateTime(2007, 10, 11) },
                new Employee { ID = 1, Name = "Robert", HireDate = new DateTime(2010, 4, 22) }
            };

            // Creating the LINQ to query the data.
            IEnumerable<Employee> query =
                from e in employees
                where e.HireDate.Year < 2005
                orderby e.Name
                select e;

            // Displaying the results of the query.
            foreach (Employee e in query)
            {
                Console.WriteLine(e.Name);
            }
            Console.WriteLine();
        }

        private static void QueryTypes()
        {
            Console.WriteLine("****LINQ Execution On Public Object Types****");

            // Creating the IEnumerable list for the LINQ query.
            IEnumerable<string> publicTypes =
                from t in Assembly.GetExecutingAssembly().GetTypes()
                where t.IsPublic
                select t.FullName;

            foreach (string name in publicTypes)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine();
        }
    }
}

In the QueryEmployee method I’m creating objects of the Employee type. Then I’m declaring an IEnumerable type of employee with a comprehension LINQ query to get all the employees with the hiredate before 2005.

The QueryTypes method is getting an IEnumerable of string on all the public types of this program. In this case the only one is the Employee class type.

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