Tech Preview: LINQ

This morning I’ve been checking out LINQ – Language Integrated Query from Microsoft, on Channel 9. LINQ enables developers to query any IEnumerable<T> collection  with a simple query, much like that of SQL.

Check out the following piece of LINQ code, which queries all the
non-static methods supported by System.String, and displays the number
of overloads for each method:

var q = from m in typeof(string).GetMethods()
where !m.IsStatic
order by m.Name
group by m.Name intp g
select new { Method = g.Key, Overloads = g.Group.Count() };

foreach (o in q)
Console.WriteLine(“{0} {1}”, o.Method, o.Overloads);

The code above defines a query q, which queries the collection,
returned by GetMethods.  The query orders on the method name,
selects only methods that are no static (notice how LINQ is strongly
typed – m.IsStatic works because m is strongly typed to the MethodInfo
class), and groups the results into a collection of objects defined on
the fly.  The count of overloads is defined by the number of
methods in each group. Lastly, the code iterates the over the
collection of objects in the query collection and displays the method
name and overloads count.

Also read about DLINQ – Language Integrated Query for Databases – and XLINQ – Language Integrated Query for XML.

LINQ was announced at PDC ’05, and is only a tech preview for
now.  The above code is likely to change before it ever reaches