Linq Refresher
Upcoming SlideShare
Loading in...5
×
 

Linq Refresher

on

  • 1,530 views

Slides from the Wellington .net User Group on Wednesday 18th February 2009

Slides from the Wellington .net User Group on Wednesday 18th February 2009

Statistics

Views

Total Views
1,530
Views on SlideShare
1,529
Embed Views
1

Actions

Likes
0
Downloads
23
Comments
0

1 Embed 1

http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Who am I? <br />
  • What does LINQ stand for? Integrated right into the middle of your coding experience. <br />
  • The most basic form of query is selecting something <br />
  • You all (i think) would have seen SQL the “other” query language <br /> LINQ has a comparable but different syntax in key areas <br /> notice the ordering of from-where-select <br />
  • First we need some other information, not strictly part of LINQ, anonymous types. <br /> What are Anonymous Types? <br />
  • Anonymouse types allow you to: <br /> this is to facilitate projections=> projecting one set of data into a related but different set <br />
  • So revisiting the original query we’re “Selecting” an Anonymous type so what do we get <br />
  • We get a container holding a whole bunch of Anonymous Types and IEnumerable where T is our anonymous types, other interfaces you may see in linq are IQueriable and IOrderedEnumerable <br />
  • So what other functions can we use LINQ to perform <br />
  • Order by: <br /> pretty similar to SQL’s ORDER BY statement <br />
  • inner and outer joins <br />
  • let statements allow you to do a function within link and assign the value to a variable, kind of like an embedded function <br />
  • 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 <br />
  • 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. <br /> Can hide mistakes or bugs, or at least throw the error much later <br />
  • eg <br />
  • <br />
  • recap <br />
  • <br />
  • <br />
  • <br />

Linq Refresher Linq Refresher Presentation Transcript

  • LINQ Refresher Owen Evans, Developer, Xero Blogger, http://bgeek.net Thursday, February 19, 2009
  • LINQ Thursday, February 19, 2009
  • Language INtegrated Query Thursday, February 19, 2009
  • SELECT lets start at the beginning Thursday, February 19, 2009
  • SELECT Structured Separated LINQ Query Language (SQL) Thursday, February 19, 2009
  • Back Up Anonymous Types Thursday, February 19, 2009
  • 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
  • Revisiting Select What does this give us? Thursday, February 19, 2009
  • Answer? An IEnumerable<T> Also See IQueriable<T> and IOrderedEnumerable<T> Thursday, February 19, 2009
  • Order get in line Thursday, February 19, 2009
  • Order By Thursday, February 19, 2009
  • Join Thursday, February 19, 2009
  • Let Thursday, February 19, 2009
  • LINQ as Extension Methods Thursday, February 19, 2009
  • 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
  • Late eval can hide errors Thursday, February 19, 2009
  • 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
  • All in one place Thursday, February 19, 2009
  • 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
  • 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
  • FIN Thursday, February 19, 2009