Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
LINQ Building Blocks
See Also
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Query a Data Model > Queries > LINQ Building Blocks

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

LINQ – Language-Integrated Query is a new feature in VisualStudio 2008 and the .NET Framework 3.5 that extends the language syntax of C# and Visual Basic by adding powerful query capabilities to it. LINQ uses easy –to-learn patterns to query data and can be extended to support potentially any kind of data.

LINQ building blocks

In this chapter we will introduce the .NET language extensions that form the basic building blocks of LINQ and then move onto actual LINQ concepts in subsequent chapters.

var – Implicitly typed local variables

C# 3.0 introduces a new keyword – var, which allows to declare a local variable without having to specify the type explicitly. When the var keyword is used to declare a local variable, the compiler infers the type of this variable from the expression on the right side of the initialization statement.

Syntax: The var keyword should be followed by the name of the local variable and then by an initializer expression.

Following are some examples of implicitly type variables along with the equivalent code in traditional syntax:

C#  Copy Code
//Integer values
var i = 10;
int i = 10;
//String values
var s = "LINQ";
string s = "LINQ";
var strings = new List<string>();
List <
string> strings = new List<string>();
VB.NET Copy Code

'Integer values

Dim i = 10
Dim i As Integer = 10

'String values

Dim s = "LINQ"
Dim s As String = "LINQ"


Dim strings = New List(Of String)()
Dim strings As New List(Of String)()

Lambda expressions

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."

Following are some examples of Lambda expressions in C#

  Copy Code
x => x + 1     
x => { return x + 1; } 
(int x) => { return x + 1;}
(x,y) => x * y   
() => Console.WriteLine();     

Lambda expressions can be assigned to delegates as follows:

  Copy Code
Func<int, bool> del = i => i > 10;


In the above example we assign the delegate 'del' of type Func<int,bool> with the lambda expression ‘i => i > 10’ .

A Lambda expression is compatible with a delegate if the following rules are respected:

  • The lambda must contain the same number of parameters as the delegate type.
  • Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.
  • The return value of the lambda, if any, must be implicitly convertible to the return type of the delegate.


For further details please refer to the MSDN link -

Extension methods

Extension methods allow us to add methods to a type after it has been defined. Extension methods are written like any other static method with the addition of the this keyword to the first parameter. The this keyword instructs the compiler to treat the method as an extension method, extending the type of the first parameter.


Example: The following method returns the count of vowels in a given string. This method extends the String class. The this keyword before String parameter denotes that this is an extension method on the String class:

C#  Copy Code
public static int VowelCount(this String source)     
int count = 0;          
char> vowels = new List<char>() { 'a', 'e', 'i', 'o', 'u', 'A',
'E', 'I', 'O', 'U' };     
foreach (char c in source)        
if (vowels.Contains(c))        

return count;          


VB.NET Copy Code
<System.Runtime.CompilerServices.Extension> _
Public Function VowelCount(source As [String]) As Integer
 Dim count As Integer = 0
 Dim vowels As New List(Of Char)()
 For Each c As Char In source
  If vowels.Contains(c) Then
   System.Math.Max(System.Threading.Interlocked.Increment(count),count - 1)
  End If
 Return count
End Function


Some advantages of extension methods are as follows:

  • A type can be extended even after it has been defined.
  • Extension methods are listed by IntelliSense and are more easily discoverable than static helper methods.
  • They make it much easier to chain operations together. 

 Anonymous types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler. Anonymous types are created using the new operator along with an object initialize.


Example: In the following example we create an anonymous type that stores the First and Last name of a person.

C# Copy Code
var person = new {FirstName = per.FirstName, LastName = per.LastName}
VB.NET Copy Code
Dim person = New With {Key .FirstName = per.FirstName, Key .LastName = per.LastName}

As can be seen from the above example, anonymous types are useful when a set of properties should be grouped together into a new type without explicitly naming and writing the new type.

The compiler considers two anonymous types that have the same number and type of properties in the same order to be the same type. They share the same compiler-generated type information.

See Also