SlideShare a Scribd company logo
UNDERSTANDING LINQ
INDEX
 Introduction to LINQ
 Why LINQ?

 Language features supporting the LINQ project

 Getting started with standard query operators

 LINQ to Objects

 Beyond basic in-memory queries

 Getting started with LINQ to SQL

 Peeking under the covers of LINQ to SQL

 Introducing LINQ to XML
INTRODUCTION TO LINQ

   Linq is short for Language Integrated Query.

   We use the term language-integrated query to indicate
    that query is an integrated feature of the developer's
    primary programming languages (for example, Visual C#,
    Visual Basic).

   Component of .NET Framework 3.5

   It is a set of methods that are defined by the
    Enumerable and Queryable classes.
LANGUAGE INTEGRATED QUERY (LINQ)

      VB                       C#                       Others…

                 .NET Language-Integrated Query

                   LINQ enabled data sources

                      LINQ enabled ADO.NET

   LINQ
                 LINQ          LINQ         LINQ            LINQ
 To Objects
              To DataSets     To SQL      To Entities      To XML


                                                           <book>
                                                             <title/>
                                                             <author/>
                                                             <price/>
                                                           </book>

 Objects                    Relational                      XML
QUERY WITHOUT LINQ

   Objects using loops and conditions

    foreach(Customer c in customers)
      if (c.Region == "UK") ...

   Databases using SQL

    SELECT * FROM Customers WHERE Region='UK‘

   XML using XPath/XQuery

    //Customers/Customer[@Region='UK']
ADO WITHOUT LINQ

SqlConnection c = new SqlConnection(…);
c.Open();
SqlCommand cmd = new SqlCommand(
                    @”SELECT c.Name, c.Phone       Query in
                     FROM Customers c               quotes
                     WHERE c.City = @p0”);
                                               Arguments loosely
cmd.Parameters[“@po”] = “London”;
                                                    bound
DataReader dr = c.Execute(cmd);
While(dr.Read()){
                                                Results loosely
    string name = r.GetString(0);                   bound
    string phone = r.GetString(1);
    Datetime date = r.GetDateTime(2);
}                                              Compiler cannot
                                                 help catch
r.Close();                                        mistakes
LIMITATIONS


o   Several steps and verbose code are required.

o   Database queries bypass all kinds of compile-time checks.

o   The SQL we write for a given DBMS is likely to fail on a
    different one.
ADVANTAGES OF USING LINQ


   Works with any data source.
   Compile-time syntax checking, and debugging support.
   IntelliSense, Design time support.
   Same basic syntax for different types of data sources.
   Providing designer tools that create object-relational
    mappings.
THE SYNTAX
                 Starts with
                    from
                                       Zero or more from,
                                       join, let, where, or
  from id in source                    orderby
{ from id in source |
  join id in source on expr equals expr [ into id ] |
  let id = expr |
                                              Ends with
  where condition |                         select or group
                                                   by
  orderby ordering, ordering, … }
  select expr | group expr by key
[ into id query ]
                               Optional into
                               continuation
LANGUAGE FEATURES SUPPORTING THE LINQ
PROJECT
LANGUAGE FEATURES
    SUPPORTING THE LINQ PROJECT
   Lambda expressions
   Extension methods
   Initialization of objects and collections in expression context
   Local types inference
   Anonymous types
   Lazy evaluation

    +Query Expressions
                                            = LINQ 
LAMBDA EXPRESSIONS

   Express the implementation of a method and the
    instantiation of a delegate from that method; they have the
    form:
    c => c + 1
    which means a function with an argument, that returns the
    value of the argument incremented by one

   The parameters of a lambda expression can be explicitly or
    implicitly typed.
Examples of lambda expressions

  x => x + 1                     // Implicitly typed, expression body
  x => { return x + 1; }         // Implicitly typed, statement body
  (int x) => x + 1               // Explicitly typed, expression body
  (int x) => { return x + 1; }   // Explicitly typed, statement body
  (x, y) => x * y                // Multiple parameters
  () => Console.WriteLine()      // No parameters

  A lambda expression is a value, that does not have a type but can be
  implicitly converted to a compatible delegate type

  delegate R Func<A,R>(A arg);
  Func<int,int> f1 = x => x + 1;                  // Ok
  Func<int,double> f2 = x => x + 1;               // Ok
  Func<double,int> f3 = x => x + 1;               // Error – double cannot be
                                                  //implicitly converted to int
EXTENSION METHODS
   Extend classes that you could not modify.

   They are defined as static methods of other classes that
    take at least one parameter, and the first parameter has
    the type of the class it extends, but preceded by the
    keyword this
EXTENSION METHODS (C#)
                                           Extension
                                           method
 namespace MyStuff
 {
    public static class Extensions
    {
      public static string Concatenate(this IEnumerable<string>
 strings,
         string separator) {…}
    }
 }
                                      Brings extensions into
                                      scope
   using MyStuff;

 string[] names = new string[] { "Jenny", "Daniel",
 "Rita" };                                                       obj.Foo(x, y)
 string s = names.Concatenate(", ");                                  
                                                               XXX.Foo(obj, x, y)

                      IntelliSense!
Example of Extension Method:

  public static int VowelCount(this String source)
  {

  int count = 0;
  List<char> vowels = new List<char>() { 'a', 'e', 'i', 'o', 'u', 'A‘,'E', 'I', 'O', 'U' };
  foreach (char c in source)
  {
    if (vowels.Contains(c))
    count++;
  }

  return count;
  }

  string name = “extension”;
  int count = name.VowelCount();
INITIALIZATION OF OBJECTS AND
  COLLECTIONS IN EXPRESSION
           CONTEXT
Object initializers let you assign values to any accessible fields or
properties of an object at creation time without having to explicitly
invoke a constructor.

Example:

private class Cat
{
    // Auto-implemented properties.
    public int Age { get; set; }
    public string Name { get; set; }
}

Cat cat = new Cat { Age = 10, Name = "Fluffy" };
Collection Initializers

  Collection initializers let you specify one or more element intializers
  when you initialize a collection class that implements IEnumerable.

  Two simple collection intializers
  List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() };


  The following collection initializer uses object initializers to initialize
  objects of the Cat class

  List<Cat> cats = new List<Cat>
  {
  new Cat(){ Name = "Sylvester", Age=8 },
  new Cat(){ Name = "Whiskers", Age=2 },
  new Cat(){ Name = "Sasha", Age=14 }
  };
Anonymous Types, Implicitly Typed variables

  An anonymous type has no name and is generated by the compiler
  based on the initialization of the object being instantiated.

  var results = from employee in employees
           join contact in contacts
           on employee.Id equals contact.Id
           select new {
              employee.FirstName, employee.LastName, contact.City };


  the part at “select new”, it’s not using any class name there to
  initialize, but it’s there and it’s anonymous

  That is the purpose of the var keyword. It infers the type of the
  object based on the data type with which it has been intialized
LOCAL VARIABLE TYPE INFERENCE (C#)

   int i = 666;
   string s = "Goodbye";
   double d = 3.14;
   int[] numbers = new int[] {1, 2, 3};
   Dictionary<int,Order> orders = new
   Dictionary<int,Order>();

   var i = 666;
   var s = "Goodbye";
   var d = 3.14;
   var numbers = new int[] {1, 2, 3};
   var orders = new Dictionary<int,Order>();


       “The type on the
       right hand side”
•   Variable type inferred from initialiser



                        Integer
                           String
      var x = 666
      var s = “Bye"      Double
      var d = 3.14
      var numbers = new Integer() {1, 2, 3}
      var orders = new Dictionary(Of Integer, Order)()
LAZY EVALUATION
   LINQ follows a lazy evaluation model
   Queries execute not when constructed, but when enumerated.
   This means you can build up a query in as many steps as you like, and it
    won't actually hit the server until you eventually start consuming the
    results.


    var query = db.Customers.Where (c => c.Name.StartsWith ("A")); query =
    query.Where (c => c.Purchases.Count() >= 2);
    var result = query.Select (c => c.Name);
    foreach (string name in result) //Only now is the query executed!
    Console.WriteLine (name);
C# 3.5 LANGUAGE INNOVATIONS

               var contacts =                         Query
                                                    expressions
                 from c in customers
                 where c.City == "Hove"
Local variable   select new { c.Name, c.Phone };
type inference

                                      Lambda
                                    expressions
                 var contacts =
                   customers
                   .Where(c => c.City == "Hove")
                   .Select(c => new { c.Name, c.Phone });
Extension
 methods          Anonymous                             Object
                    types                             initializers
GETTING STARTED WITH STANDARD QUERY
OPERATORS
GETTING STARTED WITH
STANDARD QUERY OPERATORS

   LINQ Query operators are an extension to the .NET Framework
    Class Library.

   These are a set of extension methods to perform operations in the
    context of LINQ queries.

   These operators are at the heart of the LINQ foundation and they
    are the real elements that make LINQ possible.
Some of the clauses for working with LINQ
THE FOLLOWING IS THE LIST OF STANDARD QUERY
OPERATORS ALONG WITH THEIR CATEGORY:
LINQ EXAMPLE - QUERYING AN ARRAY


//Create an array of integers
         int[] myarray = new int[] { 49, 28, 20, 15, 25,
                                     23, 24, 10, 7, 34 };

//Create a query for odd numbers
var oddNumbers = from i in myarray where i % 2 == 1 select i;

//Compose the original query to create a query for odd numbers
var sorted = from i in oddNumbers orderby i descending select i;

//Display the results of the query
foreach (int i in oddNumbers)
    Console.WriteLine(i);
Query translates to method invocation.

Where, Join, OrderBy, Select, GroupBy, …


          from c in customers
          where c.City == "Hove"
          select new { c.Name, c.Phone };


          customers
          .Where(c => c.City == "Hove")
          .Select(c => new { c.Name, c.Phone });
LINQ TO OBJECTS
LINQ TO OBJECTS
     Native query syntax in    using System;
                                using System.Query;
      C# and VB                 using System.Collections.Generic;
         IntelliSense          class app {
                                  static void Main() {
         Autocompletion            string[] names = = { "Allen", "Arthur",
                                       string[] names { "Burke", "Connor",
                                              "Frank", "Everett",
                                                "Bennett" };
     Query Operators can                     "Albert", "George",
      be used against any              IEnumerable<string> ayes };names
                                              "Harris", "David" =
                                    Func<string, bool>s[0] == 'A'); s.Length == 5;
                                           .Where(s => filter = s =>
      .NET collection               Func<string, string> extract = s => s;
                                    IEnumerable<string> expr =
      (IEnumerable<T>)              Func<string, string> s in ayes)s = s.ToUpper();
                                       foreach (string item
                                                     from project =
                                                                names
                                           Console.WriteLine(item); == 5
                                                     where s.Length
         Select, Where,            IEnumerable<string> exprs= names
                                                     orderby
                                       names[0] = "Bob";
                                           .Where(filter)
                                                     select s.ToUpper();
          GroupBy, Join, etc.              .OrderBy(extract)
                                    foreach (string item inin ayes)
                                       foreach (string item expr)
                                           .Select(project);
     Deferred Query                   Console.WriteLine(item);
                                           Console.WriteLine(item);
      Evaluation                  } foreach (string item in expr)
                                }      Console.WriteLine(item);
     Lambda Expressions          }
                                }
                                Allen
                                Arthur
                                Arthur
                                BURKE
                                DAVID
                                FRANK
BEYOND BASIC IN-MEMORY QUERIES
Querying non-generic collection

Trying to query an ArrayList using LINQ to Objects directly fails


   ArrayList books = GetArrayList();
   var query = from book in books where book.PageCount > 150
   select new { book.Title, book.Publisher.Name };


  Nongeneric collections aren’t a big problem with LINQ once you know the trick.
  The trick is to use the Cast operator
  Querying an ArrayList is possible thanks to the Cast query operator


   ArrayList books = GetArrayList();
   var query = from book in books.Cast<Book>()
   where book.PageCount > 150
   select new { book.Title, book.Publisher.Name };
   dataGridView.DataSource = query.ToList();
Grouping by multiple criteria

  var query1 = from book in SampleData.Books
  group book by book.Publisher, book.Subject;


  var query2 = from book in SampleData.Books
  group book by book.Publisher
  group book by book.Subject;


  The trick is to use an anonymous type to specify the members on which to perform the
  grouping.


  var query = from book in SampleData.Books
  group book by new { book.Publisher, book.Subject };
LINQ ARCHITECTURE
var query = from c in customers where c.City == "Hove" select c.Name;

var query = customers.Where(c => c.City == "Hove").Select(c => c.Name);


Source implements                                Source implements
IEnumerable<T>                                   IQueryable<T>

 System.Linq.Enumerable                   System.Linq.Queryable
      Delegate based                       Expression tree based




                      Objects           SQL            DataSets           Others…
GETTING STARTED WITH LINQ TO SQL
WHAT IS LINQ TO SQL?

   LINQ to SQL is an O/RM (object relational mapping) implementation
    that ships in the .NET Framework. Which allows you to model a
    relational database using .NET classes.

   You can then query the database using LINQ, as well as update/ insert/
    delete data from it.

   LINQ to SQL fully supports transactions, views, and stored procedures.

   It also provides an easy way to integrate data validation and business
    logic rules into your data model.
THE DATACONTEXT, WHAT IS IT?

   DataContext will be created for each LinqToSQL-File we
    add to our solution.

   The DataContext is the main object through which we
    will communicate with our database.

   The properties of the DataContext class are the tables
    and stored procedures in the database we
    modelled/created.
DLINQ RUNTIME SUPPORT

from c in db.Customers                       db.Customers.Add(c1);
where c.City == "London"
select c.CompanyName
                            Application      c2.City = “Seattle";
                                             db.Customers.Remove(c3);



                Enumerate      Objects    SubmitChanges()


                             LINQ to
                               SQL

                SQL Query      Rows       DML
                or SProc                  or SProcs

 SELECT CompanyName                          INSERT INTO Customer …
 FROM Customer                               UPDATE Customer …
 WHERE City = 'London'                       DELETE FROM Customer …
PEEKING UNDER THE COVERS OF LINQ TO SQL
DATA ACCESS IN DLINQ
INTRODUCTION OF LINQ TO XML
LINQ TO XML
   Large Improvement Over Existing Model

   Supports:
     Creating XML
     Loading & querying XML
     Modifying & saving XML
     Streaming, Schema, Annotations, Events
LINQ TO XML
   New XML API implemented in v3.5 assembly
       System.Xml.Linq.dll

   Namespaces
       System.Xml.Linq
       System.Xml.Schema
       System.Xml.Xpath

   Can be used independently of LINQ
KEY CLASSES IN SYSTEM.XML.LINQ
   System.Xml.Linq is a “DOM like” API
       Manipulates an XML tree in memory

   Naturally work with both XML documents and fragments

   The two key classes in System.Xml.Linq
LOADING XML CONTENT
   Loading Xml is performed with;
     XElement.Load
     XDocument.Load


   Both support loading from
       URI, XmlReader, TextReader
MODIFYING XML
   XML tree exposed by XElement is modifiable
   Modifications through methods such as:
      XElement.Add()
      XElement.Remove()
      XElement.ReplaceWith()
   Modified tree can be persisted via
      XElement.Save(), XDocument.Save()
      Both supporting filename, TextWriter, XmlWriter.
CREATING AN XML DOCUMENT
XNamespace ns = "http://example.books.com";
   XDocument books = new XDocument(
      new XElement(ns + "bookstore",
         new XElement(ns + "book",
            new XAttribute("ISBN", isbn),
            new XElement(ns + "title", "ASP.NET Book"),
            new XElement(ns + "author",
            new XElement(ns + "first-name", a.FirstName),
            new XElement(ns + "last-name", a.LastName)
         )
      )
   )
);

books.Save(@"C:Books.xml");
…MEANWHILE IN C#
   No XML Literals, But There’s
    Something to Close the Gap
   “Paste XML as XElement” Add-in
      Add XML to Clipboard
      Edit -> Past XML as XElement
   Included in VS2008 Samples
      Help -> Samples

More Related Content

What's hot

SQL Queries
SQL QueriesSQL Queries
SQL Queries
Nilt1234
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Beat Signer
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
Jeff Fox
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introductionHasan Kata
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
Thibaud Desodt
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
DrSonali Vyas
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
rainynovember12
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
Hemant Chetwani
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
Jaya Naresh Kovela
 
Json
JsonJson
ADF Bindings & Data Controls
ADF Bindings & Data ControlsADF Bindings & Data Controls
ADF Bindings & Data Controls
Rohan Walia
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
Dr.Neeraj Kumar Pandey
 
Linq to sql
Linq to sqlLinq to sql
Linq to sql
Shivanand Arur
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
Harshit Choudhary
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
IMC Institute
 
Html 5
Html 5Html 5
Html 5
Ajay Ghosh
 

What's hot (20)

SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
Json
JsonJson
Json
 
ADF Bindings & Data Controls
ADF Bindings & Data ControlsADF Bindings & Data Controls
ADF Bindings & Data Controls
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
Mysql
MysqlMysql
Mysql
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Linq to sql
Linq to sqlLinq to sql
Linq to sql
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
 
Html 5
Html 5Html 5
Html 5
 

Viewers also liked

Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course AhmedabadIntroduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
NicheTech Com. Solutions Pvt. Ltd.
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
WE-IT TUTORIALS
 
Linq
LinqLinq
Linq
samneang
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
Peter Gfader
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
Er. Kamal Bhusal
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
Rishi Kothari
 

Viewers also liked (7)

Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course AhmedabadIntroduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
Introduction Of Linq , ASP.NET Training Ahmedabad, ASP.NET Course Ahmedabad
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
 
Linq
LinqLinq
Linq
 
Linq in asp.net
Linq in asp.netLinq in asp.net
Linq in asp.net
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 

Similar to Understanding linq

Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
Neeraj Kaushik
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
Shahriar Hyder
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShakir Majeed Khan
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
pradeepkothiyal
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
CHOOSE
 
Linq intro
Linq introLinq intro
Linq intro
Bình Trọng Án
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
Christian Nagel
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Amusing C#
Amusing C#Amusing C#
Amusing C#
PVS-Studio
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
psundarau
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
Almamoon
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
mothertheressa
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
SDFG5
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
NALESVPMEngg
 
Lesson11
Lesson11Lesson11
Lesson11
Alex Honcharuk
 
DITEC - Programming with C#.NET
DITEC - Programming with C#.NETDITEC - Programming with C#.NET
DITEC - Programming with C#.NET
Rasan Samarasinghe
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 

Similar to Understanding linq (20)

Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbs
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Linq
LinqLinq
Linq
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
 
Linq intro
Linq introLinq intro
Linq intro
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Amusing C#
Amusing C#Amusing C#
Amusing C#
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
 
Lesson11
Lesson11Lesson11
Lesson11
 
DITEC - Programming with C#.NET
DITEC - Programming with C#.NETDITEC - Programming with C#.NET
DITEC - Programming with C#.NET
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 

More from Anand Kumar Rajana

Interface Vs Abstact
Interface Vs AbstactInterface Vs Abstact
Interface Vs Abstact
Anand Kumar Rajana
 
Anand's Leadership Assessment
Anand's Leadership AssessmentAnand's Leadership Assessment
Anand's Leadership Assessment
Anand Kumar Rajana
 
Sql Server 2012 Installation..
Sql Server 2012 Installation..Sql Server 2012 Installation..
Sql Server 2012 Installation..Anand Kumar Rajana
 
Json
JsonJson
Jquery Ajax
Jquery AjaxJquery Ajax
Jquery Ajax
Anand Kumar Rajana
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
Anand Kumar Rajana
 
J Query Introduction And JQuery Selectors
J Query Introduction And JQuery SelectorsJ Query Introduction And JQuery Selectors
J Query Introduction And JQuery Selectors
Anand Kumar Rajana
 

More from Anand Kumar Rajana (13)

Interface Vs Abstact
Interface Vs AbstactInterface Vs Abstact
Interface Vs Abstact
 
Anand's Leadership Assessment
Anand's Leadership AssessmentAnand's Leadership Assessment
Anand's Leadership Assessment
 
Rhino Mocks
Rhino MocksRhino Mocks
Rhino Mocks
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
The Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.NetThe Seven Pillars Of Asp.Net
The Seven Pillars Of Asp.Net
 
What Do You Mean By NUnit
What Do You Mean By NUnitWhat Do You Mean By NUnit
What Do You Mean By NUnit
 
Wcf
WcfWcf
Wcf
 
Sql Server 2012 Installation..
Sql Server 2012 Installation..Sql Server 2012 Installation..
Sql Server 2012 Installation..
 
Json
JsonJson
Json
 
Jquery Ajax
Jquery AjaxJquery Ajax
Jquery Ajax
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
J Query Introduction And JQuery Selectors
J Query Introduction And JQuery SelectorsJ Query Introduction And JQuery Selectors
J Query Introduction And JQuery Selectors
 

Recently uploaded

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 

Recently uploaded (20)

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 

Understanding linq

  • 2. INDEX  Introduction to LINQ  Why LINQ?  Language features supporting the LINQ project  Getting started with standard query operators  LINQ to Objects  Beyond basic in-memory queries  Getting started with LINQ to SQL  Peeking under the covers of LINQ to SQL  Introducing LINQ to XML
  • 3. INTRODUCTION TO LINQ  Linq is short for Language Integrated Query.  We use the term language-integrated query to indicate that query is an integrated feature of the developer's primary programming languages (for example, Visual C#, Visual Basic).  Component of .NET Framework 3.5  It is a set of methods that are defined by the Enumerable and Queryable classes.
  • 4. LANGUAGE INTEGRATED QUERY (LINQ) VB C# Others… .NET Language-Integrated Query LINQ enabled data sources LINQ enabled ADO.NET LINQ LINQ LINQ LINQ LINQ To Objects To DataSets To SQL To Entities To XML <book> <title/> <author/> <price/> </book> Objects Relational XML
  • 5. QUERY WITHOUT LINQ  Objects using loops and conditions foreach(Customer c in customers) if (c.Region == "UK") ...  Databases using SQL SELECT * FROM Customers WHERE Region='UK‘  XML using XPath/XQuery //Customers/Customer[@Region='UK']
  • 6. ADO WITHOUT LINQ SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @”SELECT c.Name, c.Phone Query in FROM Customers c quotes WHERE c.City = @p0”); Arguments loosely cmd.Parameters[“@po”] = “London”; bound DataReader dr = c.Execute(cmd); While(dr.Read()){ Results loosely string name = r.GetString(0); bound string phone = r.GetString(1); Datetime date = r.GetDateTime(2); } Compiler cannot help catch r.Close(); mistakes
  • 7. LIMITATIONS o Several steps and verbose code are required. o Database queries bypass all kinds of compile-time checks. o The SQL we write for a given DBMS is likely to fail on a different one.
  • 8. ADVANTAGES OF USING LINQ  Works with any data source.  Compile-time syntax checking, and debugging support.  IntelliSense, Design time support.  Same basic syntax for different types of data sources.  Providing designer tools that create object-relational mappings.
  • 9. THE SYNTAX Starts with from Zero or more from, join, let, where, or from id in source orderby { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | Ends with where condition | select or group by orderby ordering, ordering, … } select expr | group expr by key [ into id query ] Optional into continuation
  • 10. LANGUAGE FEATURES SUPPORTING THE LINQ PROJECT
  • 11. LANGUAGE FEATURES SUPPORTING THE LINQ PROJECT  Lambda expressions  Extension methods  Initialization of objects and collections in expression context  Local types inference  Anonymous types  Lazy evaluation +Query Expressions = LINQ 
  • 12. LAMBDA EXPRESSIONS  Express the implementation of a method and the instantiation of a delegate from that method; they have the form: c => c + 1 which means a function with an argument, that returns the value of the argument incremented by one  The parameters of a lambda expression can be explicitly or implicitly typed.
  • 13. Examples of lambda expressions x => x + 1 // Implicitly typed, expression body x => { return x + 1; } // Implicitly typed, statement body (int x) => x + 1 // Explicitly typed, expression body (int x) => { return x + 1; } // Explicitly typed, statement body (x, y) => x * y // Multiple parameters () => Console.WriteLine() // No parameters A lambda expression is a value, that does not have a type but can be implicitly converted to a compatible delegate type delegate R Func<A,R>(A arg); Func<int,int> f1 = x => x + 1; // Ok Func<int,double> f2 = x => x + 1; // Ok Func<double,int> f3 = x => x + 1; // Error – double cannot be //implicitly converted to int
  • 14. EXTENSION METHODS  Extend classes that you could not modify.  They are defined as static methods of other classes that take at least one parameter, and the first parameter has the type of the class it extends, but preceded by the keyword this
  • 15. EXTENSION METHODS (C#) Extension method namespace MyStuff { public static class Extensions { public static string Concatenate(this IEnumerable<string> strings, string separator) {…} } } Brings extensions into scope using MyStuff; string[] names = new string[] { "Jenny", "Daniel", "Rita" }; obj.Foo(x, y) string s = names.Concatenate(", ");  XXX.Foo(obj, x, y) IntelliSense!
  • 16. Example of Extension Method: public static int VowelCount(this String source) { int count = 0; List<char> vowels = new List<char>() { 'a', 'e', 'i', 'o', 'u', 'A‘,'E', 'I', 'O', 'U' }; foreach (char c in source) { if (vowels.Contains(c)) count++; } return count; } string name = “extension”; int count = name.VowelCount();
  • 17. INITIALIZATION OF OBJECTS AND COLLECTIONS IN EXPRESSION CONTEXT Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor. Example: private class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } } Cat cat = new Cat { Age = 10, Name = "Fluffy" };
  • 18.
  • 19. Collection Initializers Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable. Two simple collection intializers List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() }; The following collection initializer uses object initializers to initialize objects of the Cat class List<Cat> cats = new List<Cat> { new Cat(){ Name = "Sylvester", Age=8 }, new Cat(){ Name = "Whiskers", Age=2 }, new Cat(){ Name = "Sasha", Age=14 } };
  • 20. Anonymous Types, Implicitly Typed variables An anonymous type has no name and is generated by the compiler based on the initialization of the object being instantiated. var results = from employee in employees join contact in contacts on employee.Id equals contact.Id select new { employee.FirstName, employee.LastName, contact.City }; the part at “select new”, it’s not using any class name there to initialize, but it’s there and it’s anonymous That is the purpose of the var keyword. It infers the type of the object based on the data type with which it has been intialized
  • 21. LOCAL VARIABLE TYPE INFERENCE (C#) int i = 666; string s = "Goodbye"; double d = 3.14; int[] numbers = new int[] {1, 2, 3}; Dictionary<int,Order> orders = new Dictionary<int,Order>(); var i = 666; var s = "Goodbye"; var d = 3.14; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary<int,Order>(); “The type on the right hand side”
  • 22. Variable type inferred from initialiser Integer String var x = 666 var s = “Bye" Double var d = 3.14 var numbers = new Integer() {1, 2, 3} var orders = new Dictionary(Of Integer, Order)()
  • 23. LAZY EVALUATION  LINQ follows a lazy evaluation model  Queries execute not when constructed, but when enumerated.  This means you can build up a query in as many steps as you like, and it won't actually hit the server until you eventually start consuming the results. var query = db.Customers.Where (c => c.Name.StartsWith ("A")); query = query.Where (c => c.Purchases.Count() >= 2); var result = query.Select (c => c.Name); foreach (string name in result) //Only now is the query executed! Console.WriteLine (name);
  • 24. C# 3.5 LANGUAGE INNOVATIONS var contacts = Query expressions from c in customers where c.City == "Hove" Local variable select new { c.Name, c.Phone }; type inference Lambda expressions var contacts = customers .Where(c => c.City == "Hove") .Select(c => new { c.Name, c.Phone }); Extension methods Anonymous Object types initializers
  • 25. GETTING STARTED WITH STANDARD QUERY OPERATORS
  • 26. GETTING STARTED WITH STANDARD QUERY OPERATORS  LINQ Query operators are an extension to the .NET Framework Class Library.  These are a set of extension methods to perform operations in the context of LINQ queries.  These operators are at the heart of the LINQ foundation and they are the real elements that make LINQ possible.
  • 27. Some of the clauses for working with LINQ
  • 28. THE FOLLOWING IS THE LIST OF STANDARD QUERY OPERATORS ALONG WITH THEIR CATEGORY:
  • 29.
  • 30. LINQ EXAMPLE - QUERYING AN ARRAY //Create an array of integers int[] myarray = new int[] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 }; //Create a query for odd numbers var oddNumbers = from i in myarray where i % 2 == 1 select i; //Compose the original query to create a query for odd numbers var sorted = from i in oddNumbers orderby i descending select i; //Display the results of the query foreach (int i in oddNumbers) Console.WriteLine(i);
  • 31. Query translates to method invocation. Where, Join, OrderBy, Select, GroupBy, … from c in customers where c.City == "Hove" select new { c.Name, c.Phone }; customers .Where(c => c.City == "Hove") .Select(c => new { c.Name, c.Phone });
  • 33. LINQ TO OBJECTS  Native query syntax in using System; using System.Query; C# and VB using System.Collections.Generic;  IntelliSense class app { static void Main() {  Autocompletion string[] names = = { "Allen", "Arthur", string[] names { "Burke", "Connor", "Frank", "Everett", "Bennett" };  Query Operators can "Albert", "George", be used against any IEnumerable<string> ayes };names "Harris", "David" = Func<string, bool>s[0] == 'A'); s.Length == 5; .Where(s => filter = s => .NET collection Func<string, string> extract = s => s; IEnumerable<string> expr = (IEnumerable<T>) Func<string, string> s in ayes)s = s.ToUpper(); foreach (string item from project = names Console.WriteLine(item); == 5 where s.Length  Select, Where, IEnumerable<string> exprs= names orderby names[0] = "Bob"; .Where(filter) select s.ToUpper(); GroupBy, Join, etc. .OrderBy(extract) foreach (string item inin ayes) foreach (string item expr) .Select(project);  Deferred Query Console.WriteLine(item); Console.WriteLine(item); Evaluation } foreach (string item in expr) } Console.WriteLine(item);  Lambda Expressions } } Allen Arthur Arthur BURKE DAVID FRANK
  • 35. Querying non-generic collection Trying to query an ArrayList using LINQ to Objects directly fails ArrayList books = GetArrayList(); var query = from book in books where book.PageCount > 150 select new { book.Title, book.Publisher.Name }; Nongeneric collections aren’t a big problem with LINQ once you know the trick. The trick is to use the Cast operator Querying an ArrayList is possible thanks to the Cast query operator ArrayList books = GetArrayList(); var query = from book in books.Cast<Book>() where book.PageCount > 150 select new { book.Title, book.Publisher.Name }; dataGridView.DataSource = query.ToList();
  • 36. Grouping by multiple criteria var query1 = from book in SampleData.Books group book by book.Publisher, book.Subject; var query2 = from book in SampleData.Books group book by book.Publisher group book by book.Subject; The trick is to use an anonymous type to specify the members on which to perform the grouping. var query = from book in SampleData.Books group book by new { book.Publisher, book.Subject };
  • 37. LINQ ARCHITECTURE var query = from c in customers where c.City == "Hove" select c.Name; var query = customers.Where(c => c.City == "Hove").Select(c => c.Name); Source implements Source implements IEnumerable<T> IQueryable<T> System.Linq.Enumerable System.Linq.Queryable Delegate based Expression tree based Objects SQL DataSets Others…
  • 38. GETTING STARTED WITH LINQ TO SQL
  • 39. WHAT IS LINQ TO SQL?  LINQ to SQL is an O/RM (object relational mapping) implementation that ships in the .NET Framework. Which allows you to model a relational database using .NET classes.  You can then query the database using LINQ, as well as update/ insert/ delete data from it.  LINQ to SQL fully supports transactions, views, and stored procedures.  It also provides an easy way to integrate data validation and business logic rules into your data model.
  • 40. THE DATACONTEXT, WHAT IS IT?  DataContext will be created for each LinqToSQL-File we add to our solution.  The DataContext is the main object through which we will communicate with our database.  The properties of the DataContext class are the tables and stored procedures in the database we modelled/created.
  • 41. DLINQ RUNTIME SUPPORT from c in db.Customers db.Customers.Add(c1); where c.City == "London" select c.CompanyName Application c2.City = “Seattle"; db.Customers.Remove(c3); Enumerate Objects SubmitChanges() LINQ to SQL SQL Query Rows DML or SProc or SProcs SELECT CompanyName INSERT INTO Customer … FROM Customer UPDATE Customer … WHERE City = 'London' DELETE FROM Customer …
  • 42. PEEKING UNDER THE COVERS OF LINQ TO SQL
  • 43. DATA ACCESS IN DLINQ
  • 45. LINQ TO XML  Large Improvement Over Existing Model  Supports:  Creating XML  Loading & querying XML  Modifying & saving XML  Streaming, Schema, Annotations, Events
  • 46. LINQ TO XML  New XML API implemented in v3.5 assembly  System.Xml.Linq.dll  Namespaces  System.Xml.Linq  System.Xml.Schema  System.Xml.Xpath  Can be used independently of LINQ
  • 47. KEY CLASSES IN SYSTEM.XML.LINQ  System.Xml.Linq is a “DOM like” API  Manipulates an XML tree in memory  Naturally work with both XML documents and fragments  The two key classes in System.Xml.Linq
  • 48. LOADING XML CONTENT  Loading Xml is performed with;  XElement.Load  XDocument.Load  Both support loading from  URI, XmlReader, TextReader
  • 49. MODIFYING XML  XML tree exposed by XElement is modifiable  Modifications through methods such as:  XElement.Add()  XElement.Remove()  XElement.ReplaceWith()  Modified tree can be persisted via  XElement.Save(), XDocument.Save()  Both supporting filename, TextWriter, XmlWriter.
  • 50. CREATING AN XML DOCUMENT XNamespace ns = "http://example.books.com"; XDocument books = new XDocument( new XElement(ns + "bookstore", new XElement(ns + "book", new XAttribute("ISBN", isbn), new XElement(ns + "title", "ASP.NET Book"), new XElement(ns + "author", new XElement(ns + "first-name", a.FirstName), new XElement(ns + "last-name", a.LastName) ) ) ) ); books.Save(@"C:Books.xml");
  • 51. …MEANWHILE IN C#  No XML Literals, But There’s Something to Close the Gap  “Paste XML as XElement” Add-in  Add XML to Clipboard  Edit -> Past XML as XElement  Included in VS2008 Samples  Help -> Samples