Professional Documents
Culture Documents
LINQ as an Example
Searching in Collections
Begin with a simple array of, say, Customers.
Customer[] customers = new Customer[30]; customers[0] = new Customer(); customers[29] = new Customer();
Declarative!
LINQ = Language INtegrated Query So far, this is just list comprehension added to C#. What did it take to add list comprehension to the language?
Expressions == Methods?
Where() wants a Boolean method. The method acts as a filter. Likewise for Select(): a translation method.
Translating Expressions
Problem: Translating c.City == London to a boolean expression e, such that Where(e) is valid?
10
C# Delegates
C# delegates: method pointers. Since C# 1.0. class Demo { delegate void Foo(); void Bar() { do something }; void Test() { Foo myDelegate = new Foo(Bar); // pointer to Bar() myDelegate(); // invoke } }
11
Delegates as Arguments
Delegates can be passed as arguments.
Event handlers, jobs for threads, etc.
class Demo { void Job() { the job to carry out }; void Test() { Thread worker = new Thread( new ThreadStart(Job)); worker.start(); } }
12
Anonymous Methods
Nameless methods = on-the-fly delegates:
class Demo { delegate void Foo(); void Test() { Foo myDelegate = delegate() { do something }; myDelegate(); // invoke } }
13
string[] londoners = customers. Where(delegate(Customer c) { return c.City == London; }). Select(delegate(Customer c) { return c.Name });
14
string[] londoners = customers. Where(c => c.City == London). Select(c => c.Name);
16
Lambda Expressions
Lambda expression syntax:
Shades of ML
Wheres Where()?
We invoked Where() on Customers[]. On the resulting Customers[], we invoked Select(). New methods for arrays!?
18
Extension Methods
class Utils { public static firstChar(this string s) { return s.charAt(0); } }
So far, just a simple static method. Can be used like any other.
19
Extension Methods
But now
Using Utils; class Demo { void Foo() { string s = Hello; Console.WriteLine(s.firstChar()); } }
20
Extension Methods
Static methods that seem to extend existing types. Where(), Select(), etc. extend array types in this manner.
21
22
23
24
Example
Our code yields:
string[] londoners = customers. Where(c => c.City == London). Select(c => c.Name);
where customers is of type DbCustomers. No DbCustomers.Where(delegate(Customer c)) method exists. However: DbCustomers.Where( Expression<Func<Customer,bool>> xt)
26
Examples: 5+3*2
+ 5 *
c.city == London
== . (dot) 2 c city London
27
Expression Trees
Normally, expression trees only exist at compile-time. In C#, the compiler can create a run-time representation of the expression tree.
The language has a data type for expression trees. Represents lambda expressions at runtime.
Projections
Using LINQs select:
from c in customers where c.City == London select new AddressBookEntry(c.Name, c.Phone);
30
31
Ad-Hoc Types
new { [name1 =] expr1,, [ namen =] exprn}
Example:
33
Auto-Typed Variables
var x = 7; // x will be of type int var q = from select new {}; // q will be an array of the anonymous type Console.WriteLine(q[0].Name); Local variables only.
No auto-typing for fields or formal parameters.
34
Summary
LINQ adds static SQL expression correctness to C#. To do this, the following features were added to C#:
Lambda expressions. Extension methods. Expression types. List comprehension. Anonymous data types. Type inference.
35
Theres More
There are several LINQ features we did not present here, such as:
Grouping (GROUP BY in SQL) Joins (selecting from multiple tables) ...
39
Conclusions:
a. Learn from history. b. Know LISP, Haskell, etc: once you really understand them, it will give you serious advantages over ignorant software engineers (even if you never use these languages in practice).
40