Professional Documents
Culture Documents
Technology developed by
Anders Hejlsberg
& friends at Microsoft (2005) Presented by Tal Cohen
LINQ
A C# language extension:
Use SQL-like syntax in C#.
Outline:
Examples Understanding the witchcraft
Delegate functions Lambda expressions Type inference Anonymous types Extension methods Expression trees
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!
Translating Expressions
Problem: Translating c.City == London to an expression e, such that Where(e) is valid?
Expressions == Methods?
Where() wants a Boolean method.
The method acts as a filter. Likewise for Select(): a translation method.
# C
Delegates
10
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(); } }
11
Anonymous Methods
Nameless methods = on-the-fly delegates:
class Demo { delegate void Foo(); void Test() { Foo myDelegate = delegate() { do something }; myDelegate(); // invoke } }
12
string[] londoners = customers. Where(delegate(Customer c) { return c.City == London; }). Select(delegate(Customer c) { return c.Name });
13
14
string[] londoners = customers. Where(c => c.City == London). Select(c => c.Name);
15
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!?
17
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.
18
Extension Methods
But now
Using Utils; class Demo { void Foo() { string s = Hello; Console.WriteLine(s.firstChar()); } }
19
Extension Methods
Static methods that seem to extend existing types. Where(), Select(), etc. extend IEnumerable<T>.
Defined in System.Query.
20
21
22
23
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)
25
Expression Trees
A data type. Represents lambda expressions at runtime. Used it to generate SQL at runtime.
Guaranteed to be valid.
26
OrderData[] od = customers. Where(c => c.City == London). SelectMany(c => c.Orders. Where(o => o.OrderDate.Year == 2005). Select(o => new OrderData(c.Name, o.OrderId, o.Total)) );
28
Projections
Using LINQs select:
from c in customers where c.City == London select new AddressBookEntry(c.Name, c.Phone);
29
30
Ad-Hoc Types
Example:
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.
33
Extra
John Smith
Miss Piggy
Kermit
345529 423340
345.21 95.95
38
39
40
41
42
43
= = = = = = =
1 2 1 1 5 3 6
??
46
DLINQ
ADO.NETs next-generation. Among other features: a database-toclasses utility.
Class per DB table. Property per DB column. Foreign keys, etc. reflected in the classes.
47
48
49
XLINQ
Same idea, for searching in XML data.
50