Previewing LinQ
Just today, I tried previewing the LinQ project. If anyone of doesn’t know what the LinQ project is, it’s a new set of API’s to enable enhanced querying that is embedded to your code. LinQ stands for Language Integrated Query.
The first preview was made available to the public on last PDC 2005. Yet, I was able to see some demos lately. And this triggered mw to preview it myself.
Installing LinQ requires that you have Visual Studio 2005 or Express installed in your machine. And also since its part of the Visual Studio Orcas then you also have to download the WinFx Runtimes.
Visual Studio Express
WinFx Runtime
LinQ Preview
Upon installing don’t forget to read the ReadMe.Htm the document contains a shortcut for the installer to integrate LinQ in VS2005’s intellisense.
Upon creating a new project in VStudio, you’ll have a new type of project called LinQ Preview.
It’s getting cooler than cool!
I’ve created a new LinQ Windows Application project.
There have been several samples to guide me. Well as I have seen, my assessment so far was that the LinQ project was designed to perform SQL-based queries on your Array, List and Dictionary. So far I have created this sample code:
//Here’s my class
public class Employee {
public Employee() {
}
public string Name = "";
public string City = "";
public string Id = "";
public int Age = 0;
}
// Here’s where I used LinQ
List<Employee> employees = GetEmployees();
// this function returns a List of employees
var res =
from n in employees
where n.Age > 20
select n;
foreach(var x in res) {
MessageBox.Show(x.Name);
}
In my example, what I did was create a simple where query to get all employees in my list with an age greater that 20. Hmmm.. Never thought I could do that! Awesome!
Well, that’s not all you can do. You can perform sum, avg and other sql-based queries to your list or array. And before I forgot, all of your array, list or Dictionary now has addition LinQ specific properties such as where<>.
int[] arr_int = new int[] { 1, 2, 3, 4, 5 };
var res = arr_int.Where(n => n > 2);
The statement queries the array of integer for values less than 2. So as you can see LinQ can be very flexible. It can be used as a sql statement or as a method of an object.
Another useful capability of LinQ is the ability to query by object type. Here’s a sample:
object[] obj = new object[]{ "1", 1, 2, "x", 4 };
var i = obj.OfType<int>();
foreach(var x in i) {
MessageBox.Show(x.ToString());
}
This routine would query all integer values only excluding all values with quotes. Ain’t that cute?
Although LinQ is very impressive, it still has its limitations. You can only use it with List, Array and Dictionary. But what about DataAccess? Well, I thought that it has no capabilities. For when I tried LinQ against the Row property of DataTable it sadly failed. But alas! There’s another component that handles data thru LinQ. Its called DLinq. Its another new approach to data access. Not its not a replacement for ADO.Net. Its just another way. DLinq is also a member of the ADO.Net family. And yes, you can interoperate with it. Here’s a sample code that I did using DLinq:
// This is the class I’m using to store tables
[Table(Name="Customers")]
public class Customer
{
[Column(Id=true)] // specify that the column is a primary key
public string CustomerID;
[Column] // a normal column
public string City;
}
DataContext db = new DataContext(@"c:\employee.mdf");
// the parameter could also contain a connection string
// Retrieve a table
Table<Customer> Customers = db.GetTable<Customer>();
// Query for customers from Davao
var q =
from c in Customers
where c.City == "Davao"
select c;
foreach (var cust in q)
Console.WriteLine("id = {0}, City = {1}", cust.CustomerID, cust.City);
I think I would not go deeply here. I just wanted to show you the new capabilities of DLinq. There's also another subset in LinQ its called XLinq. It is to be used on Xml's. But I think I'll indulge on that one when I have the time. But for now, I think that this is enough to get you drooling just like me!
I’ve only tried a little bit of everything here. And I’m quite sure that LinQ would continue to be enhanced. The feature that I missed though was DataBinding. You can’t Databind these objects. But Microsoft said that databinding would be available. Hmm.. can’t wait to get my hands on Orcas!