LINQ 2 Ravi Kumar CC Team A LINQ










- Slides: 10
LINQ - 2 Ravi Kumar C++/C# Team
A LINQ Query (once again)…
Anonymous Types! �New “on the fly” class with read-only properties. var p = new { X = 0, Y = 1 }; Console. Write. Line("(X, Y) Coords = ({0}, {1})", p. X, p. Y); Console. Write. Line("The typename is {0}", p. Get. Type(). Name); <>f__Anonymous. Type 1`2[System. Int 32, System. Int 32].
Query Expression! �Set of clauses written in a new declarative syntax (SQL like). �Starts with: From clause. �Must end with: select or group clause. �Type-checked. �More clause: where, orderby, join. (etc. . ) �Source of QE: IEnumerable. �Returns: IEnumerable<T>
Contextual Keywords! �New QE clauses. �E. g. : From, select, etc… �Keywords in context of QE. int from = 0; OK Var x = from c in customer Where c. Name == “ME” Select new {c. Name, c. Age}; Query Expression
QE -> Method Invocation List<int> scores = new List<int> { 77, 98, 92, 85, 80 }; IEnumerable<int> query = from score in scores where score >= 90 select score; Compiler translation List<int> scores = new List<int> { 77, 98, 92, 85, 80 }; var query = scores. Where(score => score >= 90). Select(score => score); where and select are ext methods in System. Linq. Enumerable namespace.
Translation of QE! from score in scores where score >= 90 select score; �Source object. (scores) �where clause -> Where(…) �Parameter -> lambda exp with variable and exp in clause �scores. Where( score => score >= 90) �Enumerable. Where<int>( Func<int, bool>). �Select clause -> Select(…)
Standard Query Operators �System. Linq. Enumerable class. �Example: Where(), Select(), Order. By(), Distinct(), Average(), Max(), Min() etc… �These Extension methods are collectively known as the Standard Query Operators. �http: //msdn 2. microsoft. com/enus/vcsharp/aa 336746. aspx
Expression Trees… �Data representation of the code that a C# lambda expression would execute if the expression were compiled to IL. �parsing which the compiler generates executable code. �Parsed code that has not been compiled to IL.
Lets LINQ Together!!










