Friday 27 March 2009

LINQ in .Net

This entry is actually the one of a write up of my notes from previous DevWeeks. Although from 2008, this is still relevant today...

These notes follow on from Tim Ewald talk on this subject at DevWeek 2008.

Introduction to LINQ

LINQ stands for Language INtergrated Query, and extends the .Net framework to provide query, set and transform operations. It allows the working with of collections of objects in a SQL like fashion.

 

Lambda Expressions

Is the next step past anonymous methods (which were introduced in .Net 2.0). They are symbolised by the => operator and can have a simple test, or an entire procedure following them. They can be useful when working with any collection that is enumerable. Below is an example:

 

Person[] people = GetPeople();

IEnumerable result = people.Where(p => p.LastName == lastname)

 

Extension Methods

These allow you to extend any existing type without breaking any implementors. Extending is just like writing a new method, however for the parameters you specify "this ". The keyword "this" identifies it as an extension method, followed by the data type (could be an interface as well as a object) followed by a variable name to use in the method. The methods must be static.

 

C# 3.0 uses this technique to extend IEnumerable with the Enumerable class. This adds many functions including Sum, Avg, Min, First, Union, Where, OrderBy

public static StringExtension

{

public static string addHi(this string str)

{

return str + "Hi";

}

}

 

String name = "test";

name = name.addHi();

^Would return testHi

 

Projections

This is used in IEnumerable collections to simplify the creation of a list of one type of object from the enumeration of a list of another type of object. It's used in the Where() and Select() extension methods of IEnumerable.

 

Anonymous Types

These are structural unnamed types, that infer their type from the first assignment to them. They differ from VB6/VBA object type though as they enforce type safety from the first assignment onwards. It uses the var keyword before the name of the variable.

 

Type Inference

Type inference is a way to refer to an anonymous type. The type is inferred and not dynamic (hence type safety is enforced, as stated above)

 

They can only be used at a local scope.

var i = 5;

i = "Hello";

^this would produce an error as i is inferred as an int

 

Object Initialisers

Object Initialisers allow the property/field values of a new object to be set on instantiation. It's primarily used in the .Select() method of Ienumerable, via a lambda expression. An example is below:

 

Person[] people = GetPeople();

IEnumerable result = people.Where(p => p.LastName == lastname)

.Select(p => new PersonSimple{

Name=p.name,

LName = p.LastName});

LINQ Simplified Syntax

To simplify the use of the above features, new syntax is also supported which is more SQL like. Below is an example of the above code rewritten in this syntax

 

Person[] people GetPeople();

Var result = from p in people

where p.LastName == lastname

Select new {p.Name,LName = p.LastName}

 

LINQ Providers

The following providers are available:

LINQ to Objects

See examples above

LINQ to XML

Provides an enumerable tree model to walk through XML

LINQ to SQL

Maps Database schemas to classes

Supports queries, updates and stored procedures

Supports delegated execution (passing an ad hoc procedure to SQL to be run)

Should be used with caution - you most likely end up pulling masses of data to the server


No comments:

Post a Comment