10,000 Monkeys - Harnessing the Power of Typing Monkeys

America's 2,672,401st Most Read Blog by Kevin Marshall.
in

LINQ'd Lists

It's Tuesday, which is double post day over here at 10K Monkeys. Exciting times for those of you reading along at home. Tonight's topic  is something I've been eagar to play with, LINQ and C# 3.0.  Although its been hard to find time between the frequent cat naps and reruns of Jag. So what is LINQ? MS defines it as Unified Language Features for Object and Relational Queries. But thats not all, you can also query objects using unified language features.
 
To get started, download and install the LINQ preview. Be warned, it's not for the faint of heart.  VS 2005 doesn't fully recognize all of the syntax in intellisense and the compiler will display errors like its Y2K. Step aside compiler, I'm an IT Professional.

So open up VS 2K5 and make a new LINQ console app using one of your newly installed templates.  Then paste this little gem into the Program.cs class.

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;

namespace LINQConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetEmployees();
        }

        static void GetEmployees()
        {
            var employees = new List<Employee>() {
                new Employee { Name = "Kevin", HireDate = new DateTime(2000, 11, 18)}, 
                new Employee { Name = "Jeff", HireDate = new DateTime(1999, 5, 7)},
                new Employee { Name = "Bryan", HireDate = new DateTime(1995, 1, 12)},
                new Employee { Name = "Ryan", HireDate = new DateTime(2003, 5, 1)}, 
                new Employee { Name = "Steve", HireDate = new DateTime(2002, 12, 1)}
            };

            var rookies = 
                from r in employees
                where r.HireDate > new DateTime(2001, 1, 1) 
                orderby r.HireDate
                select r;

            var veterans = 
                from v in employees
                where v.HireDate 
< new DateTime(2001, 1, 1) 
                orderby v.HireDate
                select v;

            Console.WriteLine(
"Rookies:");
            foreach(var val1 in rookies)
            {
                Console.WriteLine(
" Name = {0}, Hire Date = {1}", val1.Name, val1.HireDate.ToShortDateString());
            }
            Console.WriteLine(
"Veterans:");
            foreach(var val2 in veterans)
            {
                Console.WriteLine(
" Name = {0}, Hire Date = {1}", val2.Name, val2.HireDate.ToShortDateString());
            }
            Console.ReadLine();
        }

    }

    class Employee
    {
        public string Name;
        public DateTime HireDate;
    }
}

Don't worry about the errors, it'll run. 
Tell the compiler "You will respect my authoritah!" (Or not depending on the location of your cubicle)

Here is the output if you don't believe me



How great is that?  So let's check out some of the new syntax. First off is the new var type. You might be thinking, at long last, the power of Javascript in C#. But no, it allows the compiler to infer the types. employees is initialized in the right hand side as a generic list of employee objects. Notice also, how the employee class doesn't need a constructor. The declartion of the list also includes several new features like populating the list directly within the curly braces without having to call add on the list and employees are created by explicitedly setting the fields (object initializers). Note, you can also set only a subset of the fields if you are too lazy to set them all. But wait, there is more, check out the sql like syntax to query a list. Behold, the same SQL syntax from 1965, is now in C#.  The great feature of the var keyword is that it allows the sql-like keywords to infer what type the query will return.

This seems like it will be a very cool addition to the next version of C#. It makes getting specific items from a list very simple.  No more for loops and if checks to find items. LINQ also has some cool syntax for creating table objects directly from a db that you can query or syntax for querying XML.  I didn't have time yet to check out performance, but I imagine it's probably not good to use huge lists.  Although being able to use object querying on a table's data is almost like having an object oriented database. When are those coming out anyway? Its amazing how slow database technology is to change. Oh, wait we can have XML in databases.  Thats a recent addition from the 1990's. I wonder what it's like to use DLINQ to query a table that has XML so that I can XLINQ that.  The possibilities are endless.

This could make a great addition to the Foos Server.  Watch out Foos DBA, I can now write inefficient queries INSIDE my app. 

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)