LINQ to FILE Provider

Language Integrated Query (or LINQ, pronounced “link” for short), is a set of .NET technologies that provide built-in language querying functionality similar to SQL, not only for database access, but for accessing data from just about any source.  Simply put LINQ is a set of language changes and API’s that allow you to write SQL-like queries natively in your chosen .NET programming language. LINQ provides the same query model and operators for different data sources, such as LINQ to Objects, LINQ to XML, LINQ to SQL and now our new LINQ to FILE provider.  And LINQ isn’t limited to traditional data sources, there are many more LINQ to “xyz”  providers, for example here is a list of some non-typical LINQ providers;

  • LINQ to WebQueries by Hartmut Maennel handles searches in the SiteSeer and MSDN Web sites.
  • LINQ to Amazon by Fabrice Marguerie, a co-author of the LINQ in Action book. LINQ to Amazon returns lists of books meeting specific criteria.
  • LINQ to Flickr by Mohammed Hossam El-Din (Bashmohandes) uses the open-source FlickrNet C# library as its infrastructure.
  • LINQ to Google Desktop supports queries to the Google desktop search engine

and the full list of available providers is much longer, if you’re interested in seeing more LINQ providers follow this link

Part of the beauty of  LINQ is that the exact same syntax is used to query (and potentially update) any data source that implements IEnumerable.  LINQ defines a set of general purpose standard query operators that allow for traversal, filtering, and projection operations to be expressed in a direct, yet declarative way in any .NET-based programming language.  Let’s take a look at it in some Clarion# code using the new LINQ to FILE provider:

In the data declaration section you declare IFileQuery instances of a file’s record type:
!  ——Data declaration section——
ClassesQuery            IFileQuery<Classes.Record>
CoursesQuery            IfileQuery<Courses.Record>

CODE
myQuery? = from class in ClassesQuery |
where class.RoomNumber < 10 |
select class

The local variable myQuery? is initialized with a query expression. A query expression operates on one (or more) information sources by applying one or more query operators from either the standard query operators or any domain-specific operators.  This expression uses two of the standard query operators: Where, and Select, and its very obvious this query will return all records where the the RoomNumber column has a value < 10.

The arguments to the Where and Select operators are called lambda expressions, which are essentially fragments of code. They allow the standard query operators to be defined individually as methods and strung together, and these methods form the basis for an extensible query language.

Lambda expressions are one important piece of the LINQ query architecture. Extension methods are the other. Extension methods combine the flexibility of dynamic languages with the performance and compile-time validation of statically-typed languages. We’ll delve into extension methods in another post.

Let’s look at another example:

myQuery? = From class in ClassesQuery |
Where class.RoomNumber < 10 |
OrderBy class.ClassNumber, class.CourseNumber Descending |
Select New {class.ClassNumber, class.CourseNumber, |
class.RoomNumber}

This query introduces the OrderBy and Select New operators. If you’ve had any exposure to SQL then this is very easy to understand, and even if you have no SQL experience its still easy to see what this query will return; a sequence of data objects where roomNumber is < 10, ordered by Class/Course numbers descending. What isn’t obvious is that “Select New” is only returning the class.ClassNumber, class.CourseNumber, and  class.RoomNumber columns.

There are many more features provided for in the new LINQ to FILE provider, here is a complete list of operators that the LINQ to File provider implements:

  • Where
  • Select
  • SelectMany
  • OrderBy/OrderByDescending
  • ThenBy/ThenByDescending
  • GroupBy
  • Join (inner)
  • GroupJoin (left)
  • Sum
  • Average
  • Min/Max
  • Count/LongCount
  • DefaultIfEmpty (usually used to provide “flat” left join)
  • Skip
  • Take
  • First/FirstOrDefault
  • Last/LastOrDefault
  • ElementAt/ElementAtOrDefault
  • Single/SingleOrDefault
  • TakeWhile
  • SkipWhile

Certainly you can already write your own code and define your own Views to provide the same  functionality, but with LINQ you don’t have to.  Besides a very simple and elegant syntax, the LINQ To FILE Provider is fast.  In our testing comparing the LINQ to FILE Provider to our plain LINQ to FILE, and to a standard VIEW structure, the tests showed the LINQ to FILE Provider was often more then twice as fast as a standard VIEW, and it always provided at least equivalent performance..

If all the LINQ to FILE Provider did was implement great query functionality it would be very powerful and very useful.  But it does much more, under the covers it implements equivalent functionality to the ABC library, except in this case its a managed code equivalent to the ABC library functionality. In a future article we’ll take a look at how we work with auto-incrementing, Insert/Update and Delete(s), and maintaining RI relations using the new LINQ to FILE Provider.

LINQ-style data access is available for all 3 .Net platforms (Winform, WebForm and Mobile). LINQ in general, and the new LINQ to FILE provider are key parts to the new templates for .Net code generation. And while you won’t need to write LINQ queries, (you’ll construct them visually and the templates will generate Dictionary related RI/AutoInc/CRUD statements for you) its still nice to know how easy it will be to create your own queries.

I’ll be doing a couple sessions on LINQ-style data access and the new LINQ to FILE provider at the upcoming Aussie DevCon, and I’ll be demonstrating how this technology fits into our code generation architecture using the new .Net templates.

4 thoughts on “LINQ to FILE Provider

  1. Pingback: Blogengine.NET Extension : Silverlight Extension - CODE - Random Things To Blog
    1. No LINQ is not deprecated by MS, it shipped in .Net 3.5 and its included as part of .Net 4. What did happen is that the MS ADO.Net team is now stating that the new (still in beta) Entity Framework (EF) will be the MS recommended data access approach. But if the Entity Framework does actually work out then no worries for LINQ, as there is a LINQ to Entities provider which consumes the objects created by the EF. Of course none of the MS insider battle over data access frameworks matters at all if you’re using the new LINQ to FILE provider.

Comments are closed.