LightSpeed supports the LINQ (Language Integrated Query) model introduced in C# 3.0 and Visual Basic 9 on .NET Framework 3.5. You can use LINQ syntax to perform LightSpeed queries and have them executed in the database where possible. This section provides a guide to using LINQ with LightSpeed; for information about the LINQ syntax and operators, see the C# or Visual Basic Language Reference, or the .NET Framework SDK.
An Example
Here is an example of a LINQ query in C#:
1var query = from m in UnitOfWork.Members 2 where m.UserName == "jb" 3 select m;
(The Visual Basic syntax is very similar, but uses VB conventions such as capitalised keywords and ”=” for the equality operator.)
Most of this is standard LINQ syntax—notice the from … where … select keywords and the use of C#’s native ”==” operator instead of the SQL ”=” operator. The only thing that is specific to LightSpeed is the object being queried: UnitOfWork.Members. We’ll come back to this below.
When you execute this statement, nothing happens immediately. LINQ waits until you try to use
“query”, for example by calling foreach(...) to iterate over it, or by calling a
standard query operator such as ToList() or Count(). At this point, LINQ hands the query over to the database
to run. In this case, it will expand to SQL along the lines of SELECT ... FROM Members WHERE
Members.UserName = 'jb'
, or SELECT COUNT(*) FROM Members WHERE Members.UserName = 'jb'
.
(The real query uses parameters instead of SQL literals.) LINQ to LightSpeed performs operations
in the database where possible, such as the WHERE filtering or the COUNT operation, so that
LINQ queries run as efficiently as hand-crafted queries.
Using LINQ to LightSpeed
The most convenient way to use LINQ to LightSpeed is via the strong typed unit of work class created for you by the designer. This allows you to use the UnitOfWork.Members syntax shown above.
1public class SampleUnitOfWork : UnitOfWork 2{ 3 public IQueryable<Member> Members 4 { 5 get { return this.Query<Member>(); } 6 } 7}
See Creating a Strong Typed Unit of Work for more information.
If you do not wish to use the designer, you can create the strong typed unit of work class yourself, or use the LINQ to LightSpeed extensions directly from your application code.
In these cases, you will use the Query<TEntity> extension method on IUnitOfWork. To gain access to this extension method, add a reference to the Mindscape.LightSpeed.Linq assembly, and add the following ‘using’ statement (Imports in Visual Basic):
1using Mindscape.LightSpeed.Linq;
Query<TEntity> returns a LINQ-queryable object like the “query” variable in the example above. So once you have an IUnitOfWork, you can perform LINQ queries against it as follows:
1var query = from m in unitOfWork.Query<Member>() 2 where m.UserName != "jb" 3 select m;
The strong typed unit of work class generated by the designer simply provides class-specific properties that wrap the Query<TEntity> method.
Refer to the “LINQ Queries” sample for examples of LINQ usage and patterns, and to Examples and Features for examples of using LINQ operators and techniques other than plain entity queries.