Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINQ - 2 Ravi Kumar C++/C# Team.

Similar presentations


Presentation on theme: "LINQ - 2 Ravi Kumar C++/C# Team."— Presentation transcript:

1 LINQ - 2 Ravi Kumar C++/C# Team

2 A LINQ Query (once again)…

3 Anonymous Types! New “on the fly” class with read-only properties.
var p = new { X = 0, Y = 1 }; Console.WriteLine("(X,Y) Coords = ({0},{1})", p.X, p.Y); Console.WriteLine("The typename is {0}",p.GetType().Name); <>f__AnonymousType1`2[System.Int32,System.Int32].

4 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>

5 Contextual Keywords! New QE clauses. Keywords in context of QE.
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

6 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.

7 Translation of QE! Source object. (scores) where clause -> Where(…)
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(…)

8 Standard Query Operators
System.Linq.Enumerable class. Example: Where(), Select(), OrderBy(), Distinct(), Average(), Max(), Min() etc… These Extension methods are collectively known as the Standard Query Operators.

9 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.

10 Lets LINQ Together!!


Download ppt "LINQ - 2 Ravi Kumar C++/C# Team."

Similar presentations


Ads by Google