What are the steps to enable LINQ in Opf3?
First, you need to download the Demo or request the Express Edition of the framework. You could also buy it because you think the framework rocks or even I rock - Buying means that you get the FULL source code of the framework and all updates for free!
Next, you need to create a Console Application (or Windows Forms Application, or even ASP.NET project) where you need to reference the Chili.Opf3 assembly and the new Chili.Opf3.Linq assembly. I have implemented all the LINQ features in an own assembly. It’s just an extension to the framework, which means you still can use the Opf3 “Core” in your .NET 2.0 projects (in Visual Studio 2005); without modifying one line of code.
Next comes the LINQ fun! To enabled LINQ you need to reference the “Chili.Opf3.Linq” namespace in the files where you want to use it. That’s because LINQ has been implemented by creating an extension method for the ObjectContext class and you won’t see the extension method without adding the namespace reference to the file!
You also need to register the storage class (that is the class that encapsulates the database for Opf3) with the so called “LinqQueryCommandBuilder”. This class is then used by the storage class to convert the Linq expression to an SQL statement (when a query comes to the storage all registered command builder are asked if they can handle the query. If none can you get an exception). This looks like this:
ObjectContext context =
// specify the command builder to have LinqQueries translated into SQL.
Making your first query looks then like this:
var query = from c in context.GetPersistents<Contact>()
LINQ for Opf3 supports also anonymous types as return values …
where c.ContactType.Type.ToUpper() == “BUYER”
, LastName = c.LastName
, Firstname = c.FirstName };
… and joins are also supported:
join u in context.GetPersistents() on c.ID equals u.ID
The LINQ extension to the Opf3 framework comes also with one helper class that holds some useful methods, like “Overlaps” (allows to check for overlapping date/time ranges), In (allows to specify a list of items and it checks the database values against them), Min, Max, Field<T> (allows to specify a field that is not found as property, but only present in the database).
You might also extend the Opf3 LINQ extension by adding own utility classes that implement the IMethodHandler interface. Such an extension would result in two new classes: one that holds the utility methods and the other that implements the IMethodHandler interface. The class that implements the IMethodHandler interface needs then to be registered with the LinqQueryCommandBuilder. The example shows the registering of a “RandomMethodHandler” that would handle the methods of a utility class that might be named “Randomizer”:
// create the linq query command builder.
var builder =
// register the method builder for the random class.
// register the query command builder with the storage
// of the ObjectContext.
The usage of the “Randomizer” looks like this:
where i.ID == Randomizer.GetNextRandom(1, 100)
Now each time a method is encountered the LinqQueryCommandBuilder is going to ask all the registered MethodHandler if they can handle the method. In our case the RandomMethodHandler (since registered) can handle the method and will do that and return the result.
For an example on how to implement something like this check out the QueryUtility and QueryUtilityMethodsHandler classes in the Chili.Opf3.Linq project.
Edit: Btw, if you are interested in what LINQ and lambda expressions in .NET are, you cold have a look at one of my older posts.