Linq Refresher


Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Who am I?
  • 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&#x2019;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&#x2019;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
  • eg

  • recap

  • Linq Refresher

    1. 1. LINQ Refresher Owen Evans, Developer, Xero Blogger, Thursday, February 19, 2009
    2. 2. LINQ Thursday, February 19, 2009
    3. 3. Language INtegrated Query Thursday, February 19, 2009
    4. 4. SELECT lets start at the beginning Thursday, February 19, 2009
    5. 5. SELECT Structured Separated LINQ Query Language (SQL) Thursday, February 19, 2009
    6. 6. Back Up Anonymous Types Thursday, February 19, 2009
    7. 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. 8. Revisiting Select What does this give us? Thursday, February 19, 2009
    9. 9. Answer? An IEnumerable<T> Also See IQueriable<T> and IOrderedEnumerable<T> Thursday, February 19, 2009
    10. 10. Order get in line Thursday, February 19, 2009
    11. 11. Order By Thursday, February 19, 2009
    12. 12. Join Thursday, February 19, 2009
    13. 13. Let Thursday, February 19, 2009
    14. 14. LINQ as Extension Methods Thursday, February 19, 2009
    15. 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. 16. Late eval can hide errors Thursday, February 19, 2009
    17. 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
    18. 18. All in one place Thursday, February 19, 2009
    19. 19. References MSDN: bb308959.aspx LINQ over REST (Astoria): 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. 20. A Few More References (thanks to Simon Green) Great for learning LINQ, includes heaps of samples Works outside of Visual Studio See the generated SQL or lambda Use LINQ to Objects in projects targeting .NET 2.0 Thursday, February 19, 2009
    21. 21. FIN Thursday, February 19, 2009