Linq inside out

482 views

Published on

Presentation about the workhorses of linq and the LINQ-pattern

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
482
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linq inside out

  1. 1. LINQ Inside OutANDRIES NIEUWENHUIZEQUADRANT SOFTWARE B.V. 16 JANUARI 2012
  2. 2. Agenda FEATURING: # LINQ-WORKHORSES (PART I) # THE LINQ-PATTERN (PART II) FROM HUMAN IN CLASS WHERE HUMAN.ISINTERESTED()SELECT HUMAN.EARS + HUMAN.EYES // MAG IK UW AANDACHT?
  3. 3. Introduction LINQ consists of:  syntax The buildingblocks: genrics, anonymous types, collection- / object- initializers, lambdas, Ienumerable’s, type inference  semantics The ideas: basic functions, lazyness / deferred execution, linq-pattern, parsing  Make sure you master them both!
  4. 4. LINQ-Workhorses AGGREGATE PROJECTION FILTERING
  5. 5. Aggregate Basisfunctie  Tekening + demo  signature: T Aggregaat<T, U>(this IEnumerable<U>, T, Func<T, U, T>) of IEnumerable<U> -> T  Synoniemen: Fold
  6. 6. Projection Basisfunctie  Tekening + demo  signature: IEnumerable<T> Projection<T, U>(this IEnumerable<U>, Func<U, T>) of: IEnumerable<U> -> IEnumerable<T>  Synoniemen: Map, Select
  7. 7. Filtering Basisfunctie  Tekening + demo  signature: IEnumerable<T> Filtering<T>(this IEnumerable<T>, Func<T, bool>) of: IEnumerable<U> -> IEnumerable<T>  Synoniemen: Where
  8. 8. The LINQ-Pattern
  9. 9. The LINQ-pattern Initializer >> [Filter] >> [Sorter] >> Projector Initializer from item in collection [Filter where filter(item)] [Sorter orderby item.propery] Projector select item + something
  10. 10. The LINQ-pattern Question Q I am trying to transform a string made of words starting with an uppercase letter. I want to separate each word with a space and keep only the first uppercase letter. All other letters should be lowercase. For example, "TheQuickBrownFox" would become "The quick brown fox". Obviously, I could use a simple foreach and build a string by checking each character, but I am trying to do it using LINQ. Would you know how to solve this elegantly using LINQ? Thank you for your help. (stackoverflow.com) Dus, "TheQuickBrownFox" -> "The Quick Brown Fox"
  11. 11. The LINQ-pattern Answer #1 public string SplitAsWords(string original) { var matches = Regex.Matches(original, "[A-Z][a-z]*") .Cast<Match>(); var str = string.Join(" ", matches .Select(match => match.Value)); str = str[0] + str.Substring(1).ToLower(); return str; } // Hier en daar wat Linq. Lastig te volgen.
  12. 12. The LINQ-pattern Answer #2 string phrase = "TheQuickBrownFox"; var invalidChars = from ch in phrase where char.IsUpper(ch) select ch; foreach (char ch in invalidChars) { int index = phrase.IndexOf(ch); phrase = phrase.Remove(index, 1); phrase = phrase.Insert(index, " " + ch); } // Projectie niet in select, maar in aparte iteratie.
  13. 13. The LINQ-pattern Answer #3 string str = (from i in Regex.Split("TheQuickBrownFox", "") select Regex.IsMatch(i, "[A-Z]") ? " " + i : i) .Aggregate((str1, str2) => str1 + str2); // Volgens pattern. Filtering en projectie samen!

×