LINQ in practice<br />”In theory there is no difference between theory and practice. But in practice there is”. Except in ...
Func<TIn1, TIn2, TOut><br />public int PlusFive(int i){<br />    return i+5;<br />}<br />Func<int, int> PlusFive = <br />d...
IEnumerable<T><br />
” Infiniteloop”<br />publicstaticIEnumerable<int> NaturalNumbers() {<br />inti=0;<br />while(true){<br />yieldreturn i++;<...
Filtered<br />publicstaticIEnumerable<int> Odds() {<br />returnNaturalNumbers().Where(i=>i%2==1);<br />}<br />
It works!<br />static void Main(string[] args)<br />{<br />    var nums = Odds().Take(5);<br />    //var executed = nums.T...
What is inside LINQ?<br />public static class FetchExtension{<br />    public static IEnumerable<T> MyWhere<T>(<br />	  th...
Imperative vs. declarative<br />var result = new List<int>();<br />foreach(var i in list){<br />    if(i % 2 == 0){<br /> ...
Upcoming SlideShare
Loading in...5
×

Linq in practice

589

Published on

Published in: Technology, Health & Medicine
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
589
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Func&lt;…&gt; is “function as type”These code blocks are equals.First type parameters are input parameters. The last type parameter is the return valueIn type declaration we already know the function type…
  • Instead of List&lt;T&gt; use the base classIEnumerable&lt;T&gt;- It is the base class for the lists (and LINQ) It doesn’t contain any kind of .RemoveAt(…) methods, so no-one will mess with your list
  • Using System.LinqFilter the ones that are not interesting
  • When you debug you notice what happens! Don’t do if(list.Count() == 0) but if(!list.Any()) so you don’t evaluate the whole list
  • Declarative is better: Higher level of abstaction Define what computer does, not howSQL-like syntax is not the point, but the functionality!SQL-like syntax is just one way to express .Where(i=&gt;i%2==0);
  • Linq in practice

    1. 1. LINQ in practice<br />”In theory there is no difference between theory and practice. But in practice there is”. Except in mathematics…<br />
    2. 2. Func<TIn1, TIn2, TOut><br />public int PlusFive(int i){<br />    return i+5;<br />}<br />Func<int, int> PlusFive = <br />delegate(int i) { return i + 5; };<br />Func<int, int> PlusFive = i => i + 5;<br />
    3. 3. IEnumerable<T><br />
    4. 4. ” Infiniteloop”<br />publicstaticIEnumerable<int> NaturalNumbers() {<br />inti=0;<br />while(true){<br />yieldreturn i++;<br />}<br />}<br />
    5. 5. Filtered<br />publicstaticIEnumerable<int> Odds() {<br />returnNaturalNumbers().Where(i=>i%2==1);<br />}<br />
    6. 6. It works!<br />static void Main(string[] args)<br />{<br />    var nums = Odds().Take(5);<br />    //var executed = nums.ToList();<br />    foreach(var i in nums) <br />        Console.WriteLine(i);<br />}<br />
    7. 7. What is inside LINQ?<br />public static class FetchExtension{<br />    public static IEnumerable<T> MyWhere<T>(<br /> this IEnumerable<T> list, Func<T, bool> cond){<br />      foreach(var i in list){<br />          if (cond(i)) yield return i;<br />      }    <br /> }<br />}<br />
    8. 8. Imperative vs. declarative<br />var result = new List<int>();<br />foreach(var i in list){<br />    if(i % 2 == 0){<br />        result.Add(i);<br />    }<br />}<br />var result =<br />    from i in list<br />    where i % 2 == 0<br />    select i;<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×