LINQ

I have been looking at LINQ (Language Integrated Query) over the past few weeks. LINQ ships with .NET Framework 3.5 which is part of Visual Studio 2008. I think of LINQ as a set of language extensions to C# (or VB.NET, etc.) that allow you to access and manipulate data from different types of data stores (SQL, XML, etc.) in a consistent way. For example, if I have a SQL database which contains a table named tblEmployees which in turn contains columns empid, last, first, and dob, then this code will populate object "results" with the data for employees whose ID is greater than or equal to 002:
 
var db = new DataContext("Server=.; Database=dbEmployees; Trusted_Connection=true");
var results = from emp in db.GetTable<TblEmployees>()
  where emp.Empid.CompareTo("002") >= 0
    select new
    {
      id = emp.Empid,
      last = emp.Last,
      first = emp.First,
      dob = emp.Dob
    };
 
However, before executing these statements I have to create a mapping class which tells my program about the structure of the SQL database. There is a command-line utility named sqlmetal.exe that can do this. There is a lot gong on in the code above including anonymous data typing (var), lambda expressions, and a new object constructor syntax. Now suppose I have similar employee data but in a text file. I could extract data with code that looks like:
 
List<Employee> employees = new List<Employee>{};
// fill object employees
var results = from emp in employees
  where emp.EmpID.CompareTo("002") >= 0
    select new
    {
      id = emp.EmpID,
      last = emp.Last,
      first = emp.First,
      dob = emp.Dob
    };
 
Notice the LINQ syntax is very similar. Again though, I have to create some mapping code, in this case an Employee class, before I can use LINQ.
 
Several things might happen with LINQ. LINQ may become almost universally used and replace older techniques for the most part. Or, LINQ may be one of those technologies that just never catch on and quickly fade into obscurity. Or, LINQ may be used only in somewhat complex coding scenarios where the overhead in using LINQ pays off with better error-checking and maintainability.
  
This entry was posted in Software Test Automation. Bookmark the permalink.