06 LINQ

1,568 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,568
On SlideShare
0
From Embeds
0
Number of Embeds
1,106
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

06 LINQ

  1. 1. LINQ
  2. 2. Iterators
  3. 3. Iteration
  4. 4. Iteration
  5. 5. What do we see• It is imperative (repetitive, verbose etc.) – Sort of awkward• This is “default” way to iterate data structure – Can be put into library• But sometimes we want to iterate differently – Determined by business rules
  6. 6. Custom iteration
  7. 7. Custom iteration
  8. 8. Custom iteration
  9. 9. The problemGiven:• Data structure knows how to iterate itself• Additionally, we want to define other ways to iterate data structureGoal:• Nice abstraction for that
  10. 10. Solutionforeach loop
  11. 11. Agenda• Manual iteration• …magic…• foreach iteration• …magic…• No iteration
  12. 12. Iterator pattern
  13. 13. Usage
  14. 14. ImplementationLet’s implement ReverseEnumerator<T>
  15. 15. ReverseEnumerator<T>
  16. 16. ReverseEnumerator<T>
  17. 17. Usage
  18. 18. Let’s make usage code identical
  19. 19. Now compiler can do iteration for us
  20. 20. Iterators are lazy (deferred) Let’s show it in debugger
  21. 21. Iteration side effects are observedeach time the sequence is iterated
  22. 22. Let’s see what else can compiler do for us
  23. 23. yield return – makes MoveNext() return true
  24. 24. yield return is also lazy Let’s check that in debugger
  25. 25. yield return is lazy
  26. 26. yield return is lazy
  27. 27. yield break – makes MoveNext() return false
  28. 28. IEnumerables are immutable We cannot change the sequenceWe can only create new sequences based on old ones
  29. 29. We can chain iteratorsRemember, the sequence is lazy
  30. 30. Infinite iterators
  31. 31. Infinite iterators
  32. 32. Let’s recall our goalGiven:• Data structure knows how to iterate itself• Additionally, we want to define other ways to iterate data structureGoal:• Nice abstraction for that
  33. 33. Iterate data structure
  34. 34. In fact, often we don’t need iteration at allTypical tasks:• Find element(s) in sequence• Check if condition is true/false for all elements• Transform sequence• Filter sequence• Aggregate sequence
  35. 35. In these cases iteration is just an implementation detailWe can express what we want declaratively
  36. 36. Generation• Empty(), Range(), Repeat()
  37. 37. Aggregation• Aggregate(), All(), Any(), Average(), Contains(), Count(), Max(), Min(), Sum()• ElementAt(), First(), Last(), Single()
  38. 38. Transformation• Cast(), OfType()• Concat(), Distinct(), Except(), Intersect(), Reverse(), Union(), Zip()• Select(), SelectMany()• GroupBy()• OrderBy(), OrderByDescending()
  39. 39. Filtration• Skip(), SkipWhile(), TakeWhile(), Where()
  40. 40. Anonymous types
  41. 41. Extension methods are niceBut we’ve got something even better!
  42. 42. LINQ(Language Integrated Queries)(a.k.a. query comprehensions)
  43. 43. select
  44. 44. where
  45. 45. select + where
  46. 46. select + where + anonymous types
  47. 47. select many
  48. 48. select many + where
  49. 49. order by
  50. 50. let
  51. 51. group by
  52. 52. group by
  53. 53. Sum up• Use LINQ and LINQ extension methods – to declaratively specify what you want – to simplify your code – to generalize your code – to create complex abstractions from simple ones• Remember that IEnumerables are lazy
  54. 54. Questions?

×