Craig Mattson (Personal Website)
Home - Blog, News, About MePrograms - C#.Net, Java, VB6MusicWebsites

Viewing News Article

LINQ - You know... just another acronym... (29/03/2009 08:43:45 PM)

crystal_clear_app_kedit_128Since the .NET Framework 3.5 was released, one buzzword floating around was LINQ (Language Integrated Query). I didn't take much note of it until tonight when I was looking for more jobs to apply for. So anyway, for those of you who haven't (and are C# or just .NET developers), this may be an interesting read for you.

What is LINQ?

If you've played with an ANSI SQL99 compliant database (read: MySQL, Oracle, MSSQL etc...), you will understand the following code:

SELECT * FROM table1 WHERE column1=expression ORDER BY column1 ASC;

The idea of LINQ is to take a similar structure and apply it to an array or list of objects. The concept though of LINQ isn't to create a list of methods (although the Language Integrated component acts as a pointer to a set of methods which I will discuss later), but to add keywords into the language to flow it similar to SQL.

Example Please!

The first and probably most easy example is to create a standard 1D array of strings. Lets assume we have Victorian suburbs / towns as the names:

string[] towns = { "Neerim South", "Caulfield", "Clayton", "Pakenham", "Drouin", "Warragul" };

These are in no particular order - so I will use LINQ to sort these into alphabetical order. However, LINQ (as far as I can tell) is only usable if the object uses the interface IEnumerable. This interface basically allows you to implement foreach iterations without requiring you to set up counters etc... Anyway, if you are using IEnumerable (or an object that implements the IEnumerable interface), you can use the following code:

IEnumerable sortedTowns = from t in towns order by t select t;

So that's just it. If you were to put a where clause in, you would do it between the from and orderby. From here, the data is sorted in alphabetical order and will return the t as the string.

What about classes?

Lets assume I have an object called Item with properties Name and Age. I will create 4 objects:

List i = new List();
i.Add(new Item() { Name = "Craig", Age = 21 });
i.Add(new Item() { Name = "Arkady", Age = 24 });
i.Add(new Item() { Name = "Claudia", Age = 21 });
i.Add(new Item() { Name = "Daniel", Age = 17 });
i.Add(new Item() { Name = "Benjamin", Age = 21 });

So now that I have my objects, lets create a query:

IEnumerable query = from item in i where item.Age > 18 orderby item.Name select new Item() { Name = item.Name, Age = item.Age };

What this does is iterate through the list and we are returning an Item (as defined by the ). Now, just like any IEnumerable class, you can use a foreach to iterate through:

foreach(Item itm in i)
{
    // Handle the Item
}

So how does it work again?

Quite simply, the keywords are mapped to methods in the IEnumerable interface. That is; OrderBy<>, Where<>, Select<> etc... are actually methods that are aliased by the use of "orderby", "where" and "select" keywords in the .NET Framework. So you could theoertically do the same thing above except use:

query.OrderBy(item.Name);

Conclusion

This LINQ stuff is pretty neat! It would have been particularly useful in one of my recent projects instead of writing a bunch of Stored Procedures to handle different sorts (or converting data to a dataset to sort). Anywho - we'll see how I go using this stuff now. Unfortunately, this is yet another one of those very handy at reducing code features that PHP is missing. Sure, I can code my classes to replicate those methods, but that takes time.

It's clearly evident that development time in ASP.Net is becoming substantially quicker than PHP, especially in data driven applications. With DataGrids, Classes, Direct Integration and Code Generation - you could build a Content Management System in hours (if you weren't worried about appearance) as opposed to a day or two in PHP.

Now all Microsoft have to do is speed up the processing of an ASP.Net website! (Oh, and stop using stupidly large amounts of RAM!)

_______________________________________

Edit: Turns out TinyMCE is stripping away my < T > and < string > when no spaces are between the < and text. For the examples above, IEnumerator should read: IEnumator< T > to resemble type, and what you are trying to store shoud have a type as well. So the first example should have IEnumerator< string > and the second: IEnumerator< Item >.

[Print View]