Linq in C# 3.0: An Overview


Published on

An Insight : whats offered by C# 3.0 in VS 2008

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Linq in C# 3.0: An Overview

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