VS 2008 - LINQ June 17, 2008 Presented By: Pradeep Kothiyal Presented To: Tech Leads\ PLs
LINQ : Overview LINQ : Features At A Glance Why LINQ? LINQ and C# - What’s New? Lets Know About NEW LINQ Operators\LINQ over Data Misc.
LINQ : Features At A Glance LINQ  LINQ is  Language Integrated Query  - a new feature added in C# 3.0 IDE Features: Multi Targeting  Intellisense support for C # 3.0 (eg. Implicitly typed local variable, query expression, extension method) Refactoring support  Language features : Implicitly typed local variables and Arrays Object Initializers Extension Methods Lambda Expression Anonymous Types Query Keywords (from, where, join, select, group into) Auto implemented properties
Why LINQ? Why LINQ? LINQ helps in: Interrogate/Manipulate data Works in Type Safe, String free manner Consistent query syntax for data, objects and XML Make simpler to work with data/object/XML LINQ and C# - What’s NEW?: Can write data access code directly Get compile time syntax and schema checking (Intellisense too) No need to create query as string, so less to worry about correct field name and syntax Data access abstraction -  Consistent syntax across various data sources -  Can join information from different data source Cont.
Why LINQ? Cont. LINQ and C# - What’s NEW? Basic query format  implicitly typed local variables Extension Method and Query Extension Methods Object Initialization Lambda Expression Anonymous Type LINQ Operators LINQ - Over Data LINQ Architecture Connect Database (SQL DB [Typed\ UnTyped Dataset], XML)
Lets Know About NEW Basic Query Format : Syntax:  from  ... < where  …  orderby … select > Example I: int [] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var   lowNums = from   n   in   numbers where   n < 5 select   n; Example II: var   oddNumbers = from   number   in   numbers where  number % 2 != 0 orderby   number   descending select  number;
Lets Know About NEW Implicitly Typed Local Variable : Must include an initializer Must be an expression, cannot be and object or collection initilizers. Complile time type of initilizer expression cannot be null Example: var   i = 5; var   s = &quot;Hello&quot;; var   d = 1.0; int [] numbers =  new int [] {1, 2, 3}; - - -  var   x;  // Error, no initializer to infer type from var   y = {1, 2, 3};  // Error, collection initializer not permitted var   z =  null ;  // Error, null type not permitted
Lets Know About NEW Extension Method : Must be declared as public Must accept a parameter, the type of which defines the class that we are extending Must exists within a static class Must include the ‘this‘ keyword in the parameter declaration Eg. If someone wants to add a method to the System.Int32 class Ref: VS 2008 Query Extension Method : Underlying query classes provide a large set of query extn. Methods. Example: -  All, Any, Average,Where, Concate, Contains
Lets Know About NEW Cont… -  ConvertAll, Count, Distinct, Except - OrderBy, ThenBy, Select etc. Lambda Expression : Acts as an Anonymous method Supply a single value and expression return value and method gets called for each items within the collection Example: foreach  ( Actor   a   in   actors. where (x => x.BirthDate.Month == 4)) Ref: VS 2008 Object Initializers : Ease construction and initialization of objects
Lets Know About NEW Anonymous Type : Needed when we want to restrict return fields or construct fields. Rather then performing these actions when we use the data (each time we use the data) creating a new type as Anonymous type when we retrieve the data Example: .. Select new {   Name = actor.FirstName + “ ” + actor.LastName , Country = actor.Country }; LINQ Operators: LINQ includes a large set of operators [extension methods] Example: - OfType, Average, Min, Max, Sum - Count, Any, All, Take, Skip
LINQ OVER DATA VB C# Others .NET Language Integrated Query LINQ  to Objects LINQ  to XML LINQ Enabled ADO.NET LINQ  to DataSet LINQ  to SQL LINQ  to Entities LINQ Enabled Data Sources <book> <book price> </book price> </book> Objects XML Relational LINQ- Arch.
LINQ OVER DATA Connect Database  : System.Data.Linq.DataContext class provides wrapper around one or more SQL server object Creates hard coded schema, mapped to original Data LINQ to SQL Classes : .dbml (Data Context Class) LINQ to Store Proc LINQ over Typed DataSet (Similar to LINQ over Objects) LINQ over UnTyped DataSet (Extra Effort) LINQ over XML
Misc. Applicability Areas Interrogate/Manipulate data Works in Type Safe, String free manner Consistent query syntax for data, objects and XML Make simpler to work with data/object/XML Known Issues and Limitation Support to SQL 2000/2005 as a lightweight O/R mapper tool but NOT to Oracle (EDM is supposed to support Oracle and other third party databases) LINQ to SQL uses optimistic concurrency Cont.
Misc. Benefit of LINQ approach Compile time checking of queries you write Allows debugging (step through of the queries) Composition of many standard query operators Allows to work with data regardless of the data source Performance Differed execution for faster search execution Learning Curve
Thank You (Pradeep Kothiyal) (pradeep.kothiyal@lntinfotech.com) (9833103221)

Linq in C# 3.0: An Overview

  • 1.
    VS 2008 -LINQ June 17, 2008 Presented By: Pradeep Kothiyal Presented To: Tech Leads\ PLs
  • 2.
    LINQ : OverviewLINQ : Features At A Glance Why LINQ? LINQ and C# - What’s New? Lets Know About NEW LINQ Operators\LINQ over Data Misc.
  • 3.
    LINQ : FeaturesAt A Glance LINQ LINQ is Language Integrated Query - a new feature added in C# 3.0 IDE Features: Multi Targeting Intellisense support for C # 3.0 (eg. Implicitly typed local variable, query expression, extension method) Refactoring support Language features : Implicitly typed local variables and Arrays Object Initializers Extension Methods Lambda Expression Anonymous Types Query Keywords (from, where, join, select, group into) Auto implemented properties
  • 4.
    Why LINQ? WhyLINQ? LINQ helps in: Interrogate/Manipulate data Works in Type Safe, String free manner Consistent query syntax for data, objects and XML Make simpler to work with data/object/XML LINQ and C# - What’s NEW?: Can write data access code directly Get compile time syntax and schema checking (Intellisense too) No need to create query as string, so less to worry about correct field name and syntax Data access abstraction - Consistent syntax across various data sources - Can join information from different data source Cont.
  • 5.
    Why LINQ? Cont.LINQ and C# - What’s NEW? Basic query format implicitly typed local variables Extension Method and Query Extension Methods Object Initialization Lambda Expression Anonymous Type LINQ Operators LINQ - Over Data LINQ Architecture Connect Database (SQL DB [Typed\ UnTyped Dataset], XML)
  • 6.
    Lets Know AboutNEW Basic Query Format : Syntax: from ... < where … orderby … select > Example I: int [] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Example II: var oddNumbers = from number in numbers where number % 2 != 0 orderby number descending select number;
  • 7.
    Lets Know AboutNEW Implicitly Typed Local Variable : Must include an initializer Must be an expression, cannot be and object or collection initilizers. Complile time type of initilizer expression cannot be null Example: var i = 5; var s = &quot;Hello&quot;; var d = 1.0; int [] numbers = new int [] {1, 2, 3}; - - - var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null ; // Error, null type not permitted
  • 8.
    Lets Know AboutNEW Extension Method : Must be declared as public Must accept a parameter, the type of which defines the class that we are extending Must exists within a static class Must include the ‘this‘ keyword in the parameter declaration Eg. If someone wants to add a method to the System.Int32 class Ref: VS 2008 Query Extension Method : Underlying query classes provide a large set of query extn. Methods. Example: - All, Any, Average,Where, Concate, Contains
  • 9.
    Lets Know AboutNEW Cont… - ConvertAll, Count, Distinct, Except - OrderBy, ThenBy, Select etc. Lambda Expression : Acts as an Anonymous method Supply a single value and expression return value and method gets called for each items within the collection Example: foreach ( Actor a in actors. where (x => x.BirthDate.Month == 4)) Ref: VS 2008 Object Initializers : Ease construction and initialization of objects
  • 10.
    Lets Know AboutNEW Anonymous Type : Needed when we want to restrict return fields or construct fields. Rather then performing these actions when we use the data (each time we use the data) creating a new type as Anonymous type when we retrieve the data Example: .. Select new { Name = actor.FirstName + “ ” + actor.LastName , Country = actor.Country }; LINQ Operators: LINQ includes a large set of operators [extension methods] Example: - OfType, Average, Min, Max, Sum - Count, Any, All, Take, Skip
  • 11.
    LINQ OVER DATAVB C# Others .NET Language Integrated Query LINQ to Objects LINQ to XML LINQ Enabled ADO.NET LINQ to DataSet LINQ to SQL LINQ to Entities LINQ Enabled Data Sources <book> <book price> </book price> </book> Objects XML Relational LINQ- Arch.
  • 12.
    LINQ OVER DATAConnect Database : System.Data.Linq.DataContext class provides wrapper around one or more SQL server object Creates hard coded schema, mapped to original Data LINQ to SQL Classes : .dbml (Data Context Class) LINQ to Store Proc LINQ over Typed DataSet (Similar to LINQ over Objects) LINQ over UnTyped DataSet (Extra Effort) LINQ over XML
  • 13.
    Misc. Applicability AreasInterrogate/Manipulate data Works in Type Safe, String free manner Consistent query syntax for data, objects and XML Make simpler to work with data/object/XML Known Issues and Limitation Support to SQL 2000/2005 as a lightweight O/R mapper tool but NOT to Oracle (EDM is supposed to support Oracle and other third party databases) LINQ to SQL uses optimistic concurrency Cont.
  • 14.
    Misc. Benefit ofLINQ approach Compile time checking of queries you write Allows debugging (step through of the queries) Composition of many standard query operators Allows to work with data regardless of the data source Performance Differed execution for faster search execution Learning Curve
  • 15.
    Thank You (PradeepKothiyal) (pradeep.kothiyal@lntinfotech.com) (9833103221)