Coding with LINQ, Patterns & Practices
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


Coding with LINQ, Patterns & Practices

Uploaded on

LINQ ja koodauskäytännöt (Finnish) ...

LINQ ja koodauskäytännöt (Finnish)

Coding with LINQ, Patterns & Practices (English)



LINQ:lle refaktorointi (Finnish)

Refactoring to LINQ (English)

F#-koodaus (Finnish)

Coding F# (English)

Source code:

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • LINQ is not actually just about collections. LINQ is context-unaware programming. You don't have to care about the inner state of a context.
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 23 23

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Coding with LINQ,Patterns & Practices LINQ & Functional ways © Tuomas Hietanen (LGPLv3)
  • 2. LINQ (Monads)• Two types of functions – Queries and Aggreagtes AGGREGATE: - returns result (Wrapper) Wrapped type, In this case IEnumerable<T> QUERY: - returns another container of the same type © Tuomas Hietanen
  • 3. LINQ SELECT = LIST.MAP (Query) MAP function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 4. LINQ WHERE= LIST.FILTER (Query) FILTER function:IEnumerable< > IEnumerable< > © Tuomas Hietanen
  • 5. LINQ AGGREGATE= LIST.FOLD (Aggregate) FOLD function:IEnumerable< > © Tuomas Hietanen
  • 6. Other Aggregates (to make things easier)Function Aggregate functionLIST.SUM Add every toLIST.COUNT Add (+1) toLIST.MAX Select bigger of or... © Tuomas Hietanen
  • 7. LINQ SELECTMANY- Not pure, has many overrides SELECT function: IEnumerable< > © Tuomas Hietanen
  • 8. LINQ SELECTMANY- Not pure, has many overrides SELECT function: © Tuomas Hietanen
  • 9. Old Legacy Way New C#, LINQ F#var result = new List<T>(); var result = from x in xs let result =foreach(var x in xs){ select … (fun x -> ...) xs result.Add(...); //same as ... var result = xs.Select(x=>...);}foreach(var x in xs){ var result = from x in xs let result = if(x=...){...} where … List.filter(fun x -> ...) xs} //same as var result = xs.Where(x=>...);var result = new T(); //many ways, based on //many ways, based onforeach(var x in xs){ var result = xs.Aggregate(…); let result = result = ...; List.fold (fun f -> ...) xs ...} //also supports unfoldforeach(var x in xs){ var result = from x in xs //SelectMany is not pure, foreach(var y in ...){ from y in … so depends on situation. .... select … E.g. List.collect, or seq {...} } //same as let result =} var result = List.collect (fun x -> ...) xs xs.SelectMany(...); © Tuomas Hietanen
  • 10. Do use lists!• Don’t return Null. Prefer throw InvalidOperationException on error and Enumerable.Empty<T>() on case of empty list• Don’t modify collections, do use new ones list.RemoveAt(0) newlist = list.Where(interestingItems => ...)• Use IEnumerable<T> not List<T> or IList<T> – It is the baseclass and default for LINQ – You can always say .toList() to evaluate the lazy collection. © Tuomas Hietanen
  • 11. Safe class design, avoid side effects• Don’t reuse variables x=x+1 y=x+1• Avoid global and class variables. Use method parameters – Better for function composition• Don’t overcapuslate layers or class structures – No coding for fun: Focus on the functionality• Declarative programming. – ”yield return” is ok in some cases, but not really declarative.• Also old design practices do still apply (=> FxCop). © Tuomas Hietanen
  • 12. F# list vs seq• List is F# default – Linked list – Good for recursive methods• Seq is IEnumerable<T> – Lazy evaluation – Good for .NET interop © Tuomas Hietanen