1 / 10

LINQ - 2

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.WriteLine ("(X,Y) Coords = ({0},{1})", p.X , p.Y );. Console.WriteLine ("The typename is {0}", p.GetType ().Name);.

ewan
Download Presentation

LINQ - 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. • 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); System.Linq.Enumerablenamespace. where and select are ext methods in

  7. 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(…)

  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. • http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx

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

More Related