understanding
linq
Linq is...
extending IEnumerable<T>
with extension methods
(which are Higher Order Functions)
that let us perform set-based operations.
Interfaces
less is more
Except when
less is....
well....
less.
Extension methods
to the rescue!
Generics
since .NET 2.0
in 2005
that's about 150 internet years
Terminology
Pay attention - this bit is important!
IEnumerable<T>
A sequence of T's.
Can *only* contain T's.
you can iterate the sequence with 'foreach'
contains an IEnumerator
- a way of moving through the sequence
- MoveNext()
- Current
- Reset()
ICollection<T>
extends IEnumerable<T>
Add(T item)
Remove(T item)
Clear()
etc...
IList<T>
extends ICollection<T>, IEnumerable<T>
understands list position:
IndexOf(T item)
Insert(int index, T item)
First-class Functions
Can you save a function to a variable?
Can that variable be passed around?
At a language level
Does C# support first-class functions?
YES!
Higher Order Functions
Takes a function as a parameter,
or
Returns a function as a result
Delegates in C#
Action
Func
Predicate
Lambdas
var lambda = id => id == 376445;
means
- create a predicate (of int)
- assign it to the variable called 'lambda'
=> can be read 'goes to'
or 'such that'
Putting it all together
extending IEnumerable<T>
with extension methods
(which are Higher Order Functions)
that let us perform set-based operations
grokking linq
By ianr
grokking linq
- 1,022