4. SELECT
lets start at the beginning
Thursday, February 19, 2009
5. SELECT
Structured Separated LINQ
Query Language (SQL)
Thursday, February 19, 2009
6. Back Up
Anonymous Types
Thursday, February 19, 2009
7. Anonymous Types
Allow you to create a new type without defining a class
two methods of declaration
explicit new{ObjectName=object.Name}
implicit new{object.Description}
new{object.Name} is the same as
new{Name=object.Name}
Thursday, February 19, 2009
8. Revisiting Select
What does this give us?
Thursday, February 19, 2009
9. Answer?
An IEnumerable<T> Also See IQueriable<T>
and
IOrderedEnumerable<T>
Thursday, February 19, 2009
15. Late Evaluation
LINQ queries are not evaluated until you need the
results (foreach, count, etc.)
Allows for optimisation and building up of the query
over time
Can produce unexpected results if you don’t know
what to expect
Thursday, February 19, 2009
16. Late eval can hide errors
Thursday, February 19, 2009
17. Things to remember about
LINQ
From Comes First (for intellisense purposes)
can be written using extension methods (helps in
exploring what is possible)
Can project to new lists of objects or anonymous
objects
Beware hard to follow LINQ
Late Evaluation: things can change, beware holding
onto the IEnumerable for too long.
Thursday, February 19, 2009
19. References
MSDN: http://msdn.microsoft.com/en-us/library/
bb308959.aspx
LINQ over REST (Astoria): http://msdn.microsoft.com/
en-us/library/bb308959.aspx
Many many ORM’s now providing LINQ support:
Lightspeed, NHibernate, LINQ to Entities (LINQ to SQL
too but abandoned)
Thursday, February 19, 2009
20. A Few More References
(thanks to Simon Green)
http://www.linqpad.net/
Great for learning LINQ, includes heaps of samples
Works outside of Visual Studio
See the generated SQL or lambda
http://www.albahari.com/nutshell/linqbridge.aspx
Use LINQ to Objects in projects targeting .NET 2.0
Thursday, February 19, 2009
What does LINQ stand for? Integrated right into the middle of your coding experience.
The most basic form of query is selecting something
You all (i think) would have seen SQL the “other” query language
LINQ has a comparable but different syntax in key areas
notice the ordering of from-where-select
First we need some other information, not strictly part of LINQ, anonymous types.
What are Anonymous Types?
Anonymouse types allow you to:
this is to facilitate projections=> projecting one set of data into a related but different set
So revisiting the original query we’re “Selecting” an Anonymous type so what do we get
We get a container holding a whole bunch of Anonymous Types and IEnumerable<T> where T is our anonymous types, other interfaces you may see in linq are IQueriable and IOrderedEnumerable<T>
So what other functions can we use LINQ to perform
Order by:
pretty similar to SQL’s ORDER BY statement
inner and outer joins
let statements allow you to do a function within link and assign the value to a variable, kind of like an embedded function
LINQ can also be done using extension methods, which may look a bit friendlier to those who don’t want to pepper their code with from syntax
A word about linq: IEnumerables are evaluated only at the time they need to be, (such as on Count, ToList, To Array, Foreach) this allows optimisation of the query you want to perform and composit building of queries.
Can hide mistakes or bugs, or at least throw the error much later