Welcome to MSDN Blogs Sign in | Join | Help
Getting started with Language Integrated Query (LINQ) in .NET Compact framework 3.5

.NET Compact framework 3.5 support s the Language Integrated querying mechanism which allows developers to query a collection of enumerable objects through a set of predefined operators. LINQ has a set of operators that allows you to traverse, filter and select an enumerable collection using a syntax that is very similar to SQL. This makes it easy for developers to perform complex querying and selecting operations on a collection of enumerable objects easily.

Enough said, let us look at a bit of code that will explain things better.

Yesterday, I bumped into a real world scenario where I had to print out the list of files in a directory that had an extension .cs or .vb. In the olden days (err back in .NET 2.0 days, not long ago J), I would’ve done something like this

string[] files = Directory.GetFiles(@"\temp");
foreach
(string file in files)
{
    FileInfo
fi = new FileInfo(file);
    if
(fi.Extension.ToLower() == ".cs" ||
          fi.Extension.ToLower() == ".vb")
        Console
.WriteLine(fi.Name);
}

 Armed with LINQ, I can do the same thing in the following way

 var files = from file in Directory.GetFiles(@"\temp")
             let
fi = new FileInfo(file)
             where
fi.Extension.ToLower() == ".cs" ||
                fi.Extension.ToLower() == ".vb"
            
select fi.Name;

 foreach (string file in files)
      Console.WriteLine(file);

 As you can see from the extremely simple example, LINQ provides a simple and an elegant way to query collection of enumerable items, in this case it is the list of files (array of strings) returned by the Directory.GetFiles method.

All right, this might be trivial and you might be wondering why would you need to consider learning this when you could’ve just as easily done this the old fashioned way.  The interesting thing (or I call it the *aha* factor) is that you could achieve more complex filtering and selection much more easily than the old fashioned way.

Consider this case where you need to not only print out the list of files that has an extension .cs or .vb, but you need to print out the list of files that start with the letter ‘s’ and has a .cs or .vb extension sorted descending .  Try doing this, the old fashioned way J

With LINQ, All I have to do is


var sortedFiles = from file in Directory.GetFiles(@"\temp")
                  let fi = new FileInfo(file)
                  where fi.Name.ToLower().StartsWith("s") &&
                  (fi.Extension.ToLower() == ".cs"
                    ||
fi.Extension.ToLower() == ".vb")
                  orderby fi.Name descending
                  select fi.Name;
  
 
foreach (string file in sortedFiles)
    Console
.WriteLine(file);

In VB.NET

Dim sortedFiles  = From file In Directory.GetFiles("\temp") _
                  
From fi = New FileInfo(file) _
                   Where
fi.Name.ToLower().StartsWith("s") And _
                  
fi.Extension.ToLower() = ".cs" _
                   Or fi.Extension.ToLower() = ".vb") _
                   Order By fi.Name Descending _
                   Select fi.Name

Voila, I was able to do this in 2 lines (technically) of simple intuitive code with LINQ. Without it, I would’ve had to create of array of string that had the list of files that match the criteria, then sort it and then print it.  By now you should see the how greatly LINQ simplifies this kind of collection based operations.  There are more complex operators supported by LINQ, for instance you can use group operator to group elements in a query. I will talk more about those operators in the future blog posts. Personally I don’t like reading blogs that span more than a single page. I like to factor things out just like we do when we code every day. So this is just an introductory post to get you guys started with LINQ.  So Jump in, download the latest version of Visual Studio code name “Orcas” and start exploring LINQ.

Note: LINQ is a new feature of VB 9.0 compiler and c# 3.0 compiler that ships with .NET Framework 3.5. Not all features that are supported by the desktop .NET Framework is supported by .NET Compact framework.

For more info, look @ http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx

 

Disclaimers:
This posting is provided "AS IS" with no warranties, and confers no rights.
The information contained within this post is in relation to beta software.  Any and all details are subject to change.

 

Posted: Thursday, May 10, 2007 3:50 AM by rajivs
Filed under:

Comments

No Comments

Anonymous comments are disabled
Page view tracker