SlideShare une entreprise Scribd logo
1  sur  287
Introducing LINQ




     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company
Objectives




         Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Objectives
• Motivate the need for LINQ




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Objectives
• Motivate the need for LINQ
• Learn about the various LINQ providers




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Objectives
• Motivate the need for LINQ
• Learn about the various LINQ providers
• Investigate simple LINQ to Objects, LINQ to
  SQL, and LINQ to XML samples




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Agenda




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query
• Some LINQ Examples




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Language Integrated Query




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Language Integrated Query
• Querying and manipulating data has always
  been a fundamental part of our jobs as
  developers




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Language Integrated Query
• Querying and manipulating data has always
  been a fundamental part of our jobs as
  developers
• Data formats change, but core needs are the
  same




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Language Integrated Query
• Querying and manipulating data has always
  been a fundamental part of our jobs as
  developers
• Data formats change, but core needs are the
  same
   Must create, retrieve, update, and delete data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Data Access
(DBASE, 1985 or so)




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Data Access
(DBASE, 1985 or so)

     USE empl

     REPLACE ALL salary WITH (salary * 1.1)
     FOR supervises > 0

     LIST ALL fname, lname, salary FOR Supervises > 0




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Data Access APIs (C#)
(late 1990s/early 2000s)




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Data Access APIs (C#)
(late 1990s/early 2000s)
      SqlCommand cmd = new SqlCommand(
        @"SELECT fname, lname, salary
           FROM Empl
           WHERE supervises > @p0"
        );

      cmd.Parameters.AddWithValue("@po", 0);

      SqlConnection c = new SqlConnection(…);
      c.Open();
      DataReader people = c.Execute(cmd);

      while (people.Read()) {
        string fname = (string) people["fname"];
        string lname = (string) people["lname"];
        double salary = (double) people["salary"];
      }
      people.Close();

              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Object/Relational Mapping (C#)
(last few years)




              Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Object/Relational Mapping (C#)
(last few years)
   public class Employee
   {
     public string FirstName;
     public string LastName;
     public double Salary;
   }

   IList employees =
   session.CreateCriteria(typeof(Employee))
     .Add(Expression.Gt("supervises", 0)
     .List();
   foreach(Employee employee in employees)
   {
     string fname = employee.FirstName;
     string lname = employee.LastName;
     double salary = employee.Salary;
   }


               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Data Access APIs (VB)
(late 1990s/early 2000s)




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Data Access APIs (VB)
(late 1990s/early 2000s)
   Dim cmd As New SqlCommand( _
     "SELECT fname, lname, salary" & _
     " FROM Empl" & _
     " WHERE supervises > @p0")

   cmd.Parameters.AddWithValue("@p0", 0)
   Dim cnn as New SqlConnection(…)
   cnn.Open()

   Dim people As DataReader = cnn.Execute(cmd)
   While people.Read()
     Dim fname As String = people("fname").ToString()
     Dim lname As String = people("lname").ToString()
     Dim salary As Double = CDbl(people("salary"))
   End While
   people.Close();


              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Object/Relational Mapping (VB)
(last few years)




             Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Object/Relational Mapping (VB)
(last few years)

   Public Class Employee
     Public FirstName As String
     Public LastName As String
     Public Salary As Double
   End Class

   Dim employees As IList = _
    session.CreateCriteria(GetType(Employee). _
    Add(Expression.Gt("supervises", 0).List()

   For Each employee As Employee In Employees
     Dim fname As String = people("fname").ToString()
     Dim lname As String = people("lname").ToString()
     Dim salary As Double = CDbl(people("salary"))
   Next employee



               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Object/Relational Mapping




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Object/Relational Mapping
• Provides:




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Object/Relational Mapping
• Provides:
   Mapping between relational data to/from objects




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Object/Relational Mapping
• Provides:
   Mapping between relational data to/from objects
   Cleaner integration of business rules and validation




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ to the Rescue




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
LINQ to the Rescue
• How do you retrieve non-relational data?




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
LINQ to the Rescue
• How do you retrieve non-relational data?
   XML, RSS, Web Services, REST, AD, Files, and so on




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
LINQ to the Rescue
• How do you retrieve non-relational data?
   XML, RSS, Web Services, REST, AD, Files, and so on
• How do you interact with plain old objects?




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
LINQ to the Rescue
• How do you retrieve non-relational data?
   XML, RSS, Web Services, REST, AD, Files, and so on
• How do you interact with plain old objects?
   How do you interact and query custom domain
    models?




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
LINQ to the Rescue
• How do you retrieve non-relational data?
   XML, RSS, Web Services, REST, AD, Files, and so on
• How do you interact with plain old objects?
   How do you interact and query custom domain
    models?
• How do you enable clean code in both a
  strongly typed and dynamic language world?




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
LINQ




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET
• Makes querying data a core programming
  concept




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET
• Makes querying data a core programming
  concept
• Works with all types and shapes of data




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET
• Makes querying data a core programming
  concept
• Works with all types and shapes of data
   Relational databases




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET
• Makes querying data a core programming
  concept
• Works with all types and shapes of data
   Relational databases
   XML




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ
• Query, Set, and Transform Operations for .NET
• Makes querying data a core programming
  concept
• Works with all types and shapes of data
   Relational databases
   XML
   Plain old objects




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
LINQ
• Works with all .NET languages




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ
• Works with all .NET languages
   VB and C# have integrated language support




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Works with all .NET languages
   VB and C# have integrated language support
• Works in any kind of project including Windows
  and Web projects




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Works with all .NET languages
   VB and C# have integrated language support
• Works in any kind of project including Windows
  and Web projects
• Also can find third party LINQ providers




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ
• Works with all .NET languages
   VB and C# have integrated language support
• Works in any kind of project including Windows
  and Web projects
• Also can find third party LINQ providers
   Amazon.com for example




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• C# uses LINQ providers to map your LINQ
  queries to the data source that you’re querying




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• C# uses LINQ providers to map your LINQ
  queries to the data source that you’re querying
• The LINQ provider takes the query that you
  create in code, and converts it into commands
  that the data source will be able to execute




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• C# uses LINQ providers to map your LINQ
  queries to the data source that you’re querying
• The LINQ provider takes the query that you
  create in code, and converts it into commands
  that the data source will be able to execute
• On return from executing the commands, the
  provider also converts the data into objects that
  create your query results



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Overview




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Overview


              LINQ enabled data sources




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Overview


                    LINQ enabled data sources



    LINQ
 To Objects




  Objects
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Overview


                    LINQ enabled data sources

                      LINQ enabled ADO.NET

    LINQ         LINQ                  LINQ                  LINQ
 To Objects   To Datasets             To SQL               To Entities




  Objects                         Relational
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Overview


                    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
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Overview
      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
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Objects




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Objects
   Allows you to query in-memory of sets of objects




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Objects
   Allows you to query in-memory of sets of objects
     o   Collections, arrays, and lists




                  Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Objects
   Allows you to query in-memory of sets of objects
     o   Collections, arrays, and lists
     o   If a class implements IEnumerable (or generic version), you
         can use LINQ to query




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL




           Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source
   Use System.Data.Linq.DataContext class to provide
    the “plumbing”




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source
   Use System.Data.Linq.DataContext class to provide
    the “plumbing”
   Can use Visual Studio’s O/R designer to make it easy




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source
   Use System.Data.Linq.DataContext class to provide
    the “plumbing”
   Can use Visual Studio’s O/R designer to make it easy
     o   For now, will mark up entity class manually




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source
   Use System.Data.Linq.DataContext class to provide
    the “plumbing”
   Can use Visual Studio’s O/R designer to make it easy
     o   For now, will mark up entity class manually
   DataContext manages communication



                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to SQL
   Allows you to query and modify data in SQL Server
   You provide mapping between a modeling class and
    schema in data source
   Use System.Data.Linq.DataContext class to provide
    the “plumbing”
   Can use Visual Studio’s O/R designer to make it easy
     o   For now, will mark up entity class manually
   DataContext manages communication
     o   Stores state for optimistic concurrency checks


                 Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML




           Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content
   Can work with XML from file, or from stream




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content
   Can work with XML from file, or from stream
   System.Linq.Xml.XObject, Xnode, XElement,
    XAttribute




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content
   Can work with XML from file, or from stream
   System.Linq.Xml.XObject, Xnode, XElement,
    XAttribute
     o   Make it easy to work with XML programmatically




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content
   Can work with XML from file, or from stream
   System.Linq.Xml.XObject, Xnode, XElement,
    XAttribute
     o   Make it easy to work with XML programmatically
   LINQ to XML provider works with these classes to
    create queryable collections of XML data




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to XML
   Easy to query, modify, and generate XML content
   Can work with XML from file, or from stream
   System.Linq.Xml.XObject, Xnode, XElement,
    XAttribute
     o   Make it easy to work with XML programmatically
   LINQ to XML provider works with these classes to
    create queryable collections of XML data
   Visual Basic adds language-specific rich support for
    LINQ to XML


                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet
   Allows you to query and update data in ADO.NET
    dataset




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet
   Allows you to query and update data in ADO.NET
    dataset
   Can use querying engine against data already cached
    in client application




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet
   Allows you to query and update data in ADO.NET
    dataset
   Can use querying engine against data already cached
    in client application
   Strongly typed datasets easier to work with than
    standard datasets




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet
   Allows you to query and update data in ADO.NET
    dataset
   Can use querying engine against data already cached
    in client application
   Strongly typed datasets easier to work with than
    standard datasets
     o   You already know the schema—it's just LINQ to Objects




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to DataSet
   Allows you to query and update data in ADO.NET
    dataset
   Can use querying engine against data already cached
    in client application
   Strongly typed datasets easier to work with than
    standard datasets
     o   You already know the schema—it's just LINQ to Objects
   Language features make it possible to query
    standard datasets


                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities
   Allows developers to query data exposed using Entity
    Data Model (EDM)




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities
   Allows developers to query data exposed using Entity
    Data Model (EDM)
     o   EDM: conceptual model, allows applications to interact with
         data as if it was a collection of objects, or entities




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities
   Allows developers to query data exposed using Entity
    Data Model (EDM)
     o   EDM: conceptual model, allows applications to interact with
         data as if it was a collection of objects, or entities
   Using EDM, ADO.NET exposes entities as objects
    in .NET Framework, allows LINQ queries to operate




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities
   Allows developers to query data exposed using Entity
    Data Model (EDM)
     o   EDM: conceptual model, allows applications to interact with
         data as if it was a collection of objects, or entities
   Using EDM, ADO.NET exposes entities as objects
    in .NET Framework, allows LINQ queries to operate
   Conceptually, same as LINQ to SQL, but works with
    any data source




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
LINQ Providers
• LINQ to Entities
   Allows developers to query data exposed using Entity
    Data Model (EDM)
     o   EDM: conceptual model, allows applications to interact with
         data as if it was a collection of objects, or entities
   Using EDM, ADO.NET exposes entities as objects
    in .NET Framework, allows LINQ queries to operate
   Conceptually, same as LINQ to SQL, but works with
    any data source
     o   Doesn't assume one-to-one mapping between classes and
         schema


                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Agenda




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query
• Some LINQ Examples




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Structure of a LINQ Query




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
   Filtering




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
   Filtering
   Sorting




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
   Filtering
   Sorting
   Grouping




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
     Filtering
     Sorting
     Grouping
     Joining




                  Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
     Filtering
     Sorting
     Grouping
     Joining
     Calculating




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Structure of a LINQ Query
• LINQ query contains clauses that specify data
  sources and iteration variables
• Query expressions can also include:
     Filtering
     Sorting
     Grouping
     Joining
     Calculating
• From required—indicates source of data

                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
The Three Stages of a LINQ Query




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
The Three Stages of a LINQ Query
• Each LINQ query requires three stages:




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The Three Stages of a LINQ Query
• Each LINQ query requires three stages:
   Obtain the data source(s)




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Three Stages of a LINQ Query
• Each LINQ query requires three stages:
   Obtain the data source(s)
   Create the query




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Three Stages of a LINQ Query
• Each LINQ query requires three stages:
   Obtain the data source(s)
   Create the query
   Execute the query




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Data Source




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
The Data Source
• In the sample, data source is an array of
  integers




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Data Source
• In the sample, data source is an array of
  integers
• Because array implements IEnumerable, can
  query against it




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The Data Source
• In the sample, data source is an array of
  integers
• Because array implements IEnumerable, can
  query against it
   Treated as a "queryable" type




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
XML as Data Source (VB)



' VB
Dim items As XElement = XElement.Load("C:Grocery.xml")




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
XML as Data Source (VB)
• Could use LINQ to XML to load contents of an
  XML file into a queryable collection of XElement
  instances:

 ' VB
 Dim items As XElement = XElement.Load("C:Grocery.xml")




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
XML as Data Source (C#)



// C#:
XElement items = XElement.Load(@"C:Grocery.xml");




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
XML as Data Source (C#)
• Could use LINQ to XML to load contents of an
  XML file into a queryable collection of XElement
  instances:

 // C#:
 XElement items = XElement.Load(@"C:Grocery.xml");




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
SQL as Data Source (VB)


' VB
Dim dc As New DataContext( _
  My.Settings.NorthwindConnectionString)
Dim customers As Table(Of Customer) = _
  dc.GetTable(Of Customer)




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
SQL as Data Source (VB)
• Can use LINQ to SQL once you have a class that
  provides object-relational mapping:
 ' VB
 Dim dc As New DataContext( _
   My.Settings.NorthwindConnectionString)
 Dim customers As Table(Of Customer) = _
   dc.GetTable(Of Customer)




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
SQL as Data Source (C#)


 // C#
 DataContext dc = new DataContext(
  Properties.Settings.Default.NorthwindConnectionString);
 Table<Customer> customers = dc.GetTable<Customer>();




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
SQL as Data Source (C#)
• Can use LINQ to SQL once you have a class that
  provides object-relational mapping:

  // C#
  DataContext dc = new DataContext(
   Properties.Settings.Default.NorthwindConnectionString);
  Table<Customer> customers = dc.GetTable<Customer>();




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
The Query




        Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
The Query
• Given the data source, can specify the query




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Query
• Given the data source, can specify the query
   Defines the information you want to retrieve




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Query
• Given the data source, can specify the query
   Defines the information you want to retrieve
   Optionally, can specify how to sort, filter, and group




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Query
• Given the data source, can specify the query
   Defines the information you want to retrieve
   Optionally, can specify how to sort, filter, and group
• LINQ queries are generally the same, no matter
  the data source




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Query Execution




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
The Query Execution
• Query definition doesn't execute the query




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Query Execution
• Query definition doesn't execute the query
• Instead, defines the query that will run when it's
  time to retrieve the data




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Query Execution
• Query definition doesn't execute the query
• Instead, defines the query that will run when it's
  time to retrieve the data
• To execute, need to run code that forces query
  reference to require the values




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Query Execution
• Query definition doesn't execute the query
• Instead, defines the query that will run when it's
  time to retrieve the data
• To execute, need to run code that forces query
  reference to require the values
   In this course, For Each loop generally forces




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Multiple Values




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?
• In general, two options:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?
• In general, two options:
   Create class that exposes properties




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?
• In general, two options:
   Create class that exposes properties
     o   Specify new instance of class in Select




                 Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?
• In general, two options:
   Create class that exposes properties
     o   Specify new instance of class in Select
   Specify values in Select clause




                 Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Returning Multiple Values
• Want to return multiple values?
• In general, two options:
   Create class that exposes properties
     o   Specify new instance of class in Select
   Specify values in Select clause
     o   Compiler creates a new anonymous type containing just the
         properties you specify




                 Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Creating a Class for Return Values




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Creating a Class for Return Values
• See MyFileInfo class and MultipleReturnValues1




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query
   Why create a class simply to return values?




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query
   Why create a class simply to return values?
• Anonymous types generally simpler




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query
   Why create a class simply to return values?
• Anonymous types generally simpler
• Compiler creates the type, based on values
  specified in query Select clause




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query
   Why create a class simply to return values?
• Anonymous types generally simpler
• Compiler creates the type, based on values
  specified in query Select clause
   You don't know the type's name



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using an Anonymous Type
• In previous example, code created a simple type
  (MyFileInfo)
   Type contained properties returned by query
   Why create a class simply to return values?
• Anonymous types generally simpler
• Compiler creates the type, based on values
  specified in query Select clause
   You don't know the type's name
   Implicit type definition makes it possible to work with
    the anonymous type

              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Deferred Execution




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:
   Creating query doesn't return data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:
   Creating query doesn't return data
   Provides potential for returning data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:
   Creating query doesn't return data
   Provides potential for returning data
• Can take advantage of deferred execution




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:
   Creating query doesn't return data
   Provides potential for returning data
• Can take advantage of deferred execution
   Break query into small steps without fear of multiple
    trips to the server for data



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Deferred Execution
• Defining query doesn't cause query to execute
• Must actually force execution by requesting data
• In other words:
   Creating query doesn't return data
   Provides potential for returning data
• Can take advantage of deferred execution
   Break query into small steps without fear of multiple
    trips to the server for data
   Makes debugging simpler, as well


              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Converting Results to Force Execution




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Converting Results to Force Execution
• Sometimes, might want to immediately execute
  query and cache its results




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Converting Results to Force Execution
• Sometimes, might want to immediately execute
  query and cache its results
• Can use method like ToList or ToArray




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Converting Results to Force Execution
• Sometimes, might want to immediately execute
  query and cache its results
• Can use method like ToList or ToArray
• Each executes query, and returns results




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Agenda




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Agenda
• Language Integrated Query
• Structure of a LINQ Query
• Some LINQ Examples




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Some LINQ Examples




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ
• Introduce some of the new language features
  for LINQ




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ
• Introduce some of the new language features
  for LINQ
• Examples of:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ
• Introduce some of the new language features
  for LINQ
• Examples of:
   LINQ to Objects




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ
• Introduce some of the new language features
  for LINQ
• Examples of:
   LINQ to Objects
   LINQ to SQL




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Some LINQ Examples
• Get a taste of the kinds of things you can do
  with LINQ
• Introduce some of the new language features
  for LINQ
• Examples of:
   LINQ to Objects
   LINQ to SQL
   LINQ to XML



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to Objects




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ to Objects
• Can use any collection that implements
  IEnumerable (or generic version) as data source




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to Objects
• Can use any collection that implements
  IEnumerable (or generic version) as data source
• Note example uses multiple overloaded versions
  of DisplayResults procedure




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(C#)




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(C#)
• How does language compiler turn keywords like
  select, where, and orderby into executable
  code?




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(C#)
• How does language compiler turn keywords like
  select, where, and orderby into executable
  code?
   Keywords need to be converted into methods




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(C#)
• How does language compiler turn keywords like
  select, where, and orderby into executable
  code?
   Keywords need to be converted into methods
• Each LINQ keyword corresponds to a single
  method of the System.Linq.Enumerable




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(VB)




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(VB)
• How does language compiler turn keywords like
  Select, Where, and Order By into executable
  code?




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(VB)
• How does language compiler turn keywords like
  Select, Where, and Order By into executable
  code?
   Keywords need to be converted into methods




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Query Syntax/Extension Methods
(VB)
• How does language compiler turn keywords like
  Select, Where, and Order By into executable
  code?
   Keywords need to be converted into methods
• Each LINQ keyword corresponds to a single
  method of the System.Linq.Enumerable




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Continuing the Investigation




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on
   Where do those methods come from?




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on
   Where do those methods come from?
• LINQ requires ability to extend existing
  interfaces/classes without changing their
  definitions




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on
   Where do those methods come from?
• LINQ requires ability to extend existing
  interfaces/classes without changing their
  definitions
   Available because of new extension methods



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on
   Where do those methods come from?
• LINQ requires ability to extend existing
  interfaces/classes without changing their
  definitions
   Available because of new extension methods
• Extension method exists in one class

              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Continuing the Investigation
• So compiler converts keywords into methods
• But IEnumerable is an old interface
   It doesn't support Where, Select, and so on
   Where do those methods come from?
• LINQ requires ability to extend existing
  interfaces/classes without changing their
  definitions
   Available because of new extension methods
• Extension method exists in one class
   Allows code to extend a different class
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (VB)




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending
   Exist within a module




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (VB)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending
   Exist within a module
   Include the <Extension()> attribute




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (C#)




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending
   Exist within a static class




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending
   Exist within a static class
   Be a static method




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Extension Methods (C#)
• Add new methods to existing classes
• Not just for LINQ, but crucial for LINQ
• Extension methods must:
   Accept a parameter, the type of which defines the
    class that you’re extending
   Exist within a static class
   Be a static method
   Include the this keyword in the parameter




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Using LINQ to Group Output




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Using LINQ to Group Output
• System.Linq.Enumerable class provides large
  number of methods




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using LINQ to Group Output
• System.Linq.Enumerable class provides large
  number of methods
• Look for grouping methods




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ to SQL




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties
• To support LINQ to SQL, must add custom .NET
  attributes to class that represents the schema




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties
• To support LINQ to SQL, must add custom .NET
  attributes to class that represents the schema
• Map a class to a table




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties
• To support LINQ to SQL, must add custom .NET
  attributes to class that represents the schema
• Map a class to a table
    Map properties within the class to columns




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties
• To support LINQ to SQL, must add custom .NET
  attributes to class that represents the schema
• Map a class to a table
    Map properties within the class to columns
    All by adding attributes


                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
LINQ to SQL
• Allows standard LINQ queries to work with data
  stored in relational databases
    Extends ADO.NET
    Adds support for mapping tables and rows to classes and
     properties
• To support LINQ to SQL, must add custom .NET
  attributes to class that represents the schema
• Map a class to a table
    Map properties within the class to columns
    All by adding attributes
• Can represent relationships as well
                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Linking a Class to SQL Server




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Linking a Class to SQL Server
• Retrieve data from Northwind Customers table




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Linking a Class to SQL Server
• Retrieve data from Northwind Customers table
• Retrieve CustomerID, CompanyName,
  ContactName, Country, Region




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Linking a Class to SQL Server
• Retrieve data from Northwind Customers table
• Retrieve CustomerID, CompanyName,
  ContactName, Country, Region
• Indicate primary key, columns, and rename
  column




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Creating the Class




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Creating the Class
• Start by creating a class with properties that
  correspond to columns in the database




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating the Class
• Start by creating a class with properties that
  correspond to columns in the database
• See Customer1 class




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Marking Up the Class




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Marking Up the Class
• Use attributes from the
  System.Data.Linq.Mapping namespace




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Marking Up the Class
• Use attributes from the
  System.Data.Linq.Mapping namespace
   Table




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Marking Up the Class
• Use attributes from the
  System.Data.Linq.Mapping namespace
   Table
   Column




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Marking Up the Class
• Use attributes from the
  System.Data.Linq.Mapping namespace
   Table
   Column
   Many others




             Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Marking Up the Class
• Use attributes from the
  System.Data.Linq.Mapping namespace
   Table
   Column
   Many others
• See Customer class




             Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Retrieving a Collection of Customers




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data
   Need some class that converts requests for data into
    SQL strings, and executes/virtualizes




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data
   Need some class that converts requests for data into
    SQL strings, and executes/virtualizes
• System.Data.Linq.DataContext class




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data
   Need some class that converts requests for data into
    SQL strings, and executes/virtualizes
• System.Data.Linq.DataContext class
   Includes members that provide access to data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data
   Need some class that converts requests for data into
    SQL strings, and executes/virtualizes
• System.Data.Linq.DataContext class
   Includes members that provide access to data
   Tracks changes to data for updates



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Retrieving a Collection of Customers
• Need some mechanism to manage connection
  between class and data
   Can't simply fill some huge data structure with all the
    data
   Need some class that converts requests for data into
    SQL strings, and executes/virtualizes
• System.Data.Linq.DataContext class
   Includes members that provide access to data
   Tracks changes to data for updates
   O/R designer creates classes, and new class that
    inherits from DataContext
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Determining the Actual SQL String




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Determining the Actual SQL String
• Set Log property of DataContext to stream




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Determining the Actual SQL String
• Set Log property of DataContext to stream
   Sends all information to the output stream




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Determining the Actual SQL String
• Set Log property of DataContext to stream
   Sends all information to the output stream
• To simply see the SQL string




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Determining the Actual SQL String
• Set Log property of DataContext to stream
   Sends all information to the output stream
• To simply see the SQL string
   Call ToString method of query variable




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Working with Anonymous Types




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Working with Anonymous Types
• Anonymous types work well with LINQ to SQL




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Working with Anonymous Types
• Anonymous types work well with LINQ to SQL
• Often need to retrieve just a subset of columns




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Working with Anonymous Types
• Anonymous types work well with LINQ to SQL
• Often need to retrieve just a subset of columns
   Or perform a calculation




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Working with Anonymous Types
• Anonymous types work well with LINQ to SQL
• Often need to retrieve just a subset of columns
   Or perform a calculation
• See DemoLinqToSqlWithAnonymousTypes




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
LINQ to XML




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath
• What if you could accomplish these using a
  standard syntax?




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath
• What if you could accomplish these using a
  standard syntax?
• That is, what about LINQ?




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath
• What if you could accomplish these using a
  standard syntax?
• That is, what about LINQ?
• LINQ to XML makes it possible


             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath
• What if you could accomplish these using a
  standard syntax?
• That is, what about LINQ?
• LINQ to XML makes it possible
• Examples create XML content

             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
LINQ to XML
• Need to retrieve, create, query XML?
• .NET Framework provides rich support
   Steps can be complex
   Can require XSLT and XPath
• What if you could accomplish these using a
  standard syntax?
• That is, what about LINQ?
• LINQ to XML makes it possible
• Examples create XML content
   Examine Course class, and CreateCourseList
             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Life without LINQ to XML




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Life without LINQ to XML
• Without LINQ to XML, creating XML content
  required multiple steps




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Life without LINQ to XML
• Without LINQ to XML, creating XML content
  required multiple steps
   XmlDocument creates new elements




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Life without LINQ to XML
• Without LINQ to XML, creating XML content
  required multiple steps
   XmlDocument creates new elements
   Set properties




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Life without LINQ to XML
• Without LINQ to XML, creating XML content
  required multiple steps
   XmlDocument creates new elements
   Set properties
   Append as child of new parent




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Life without LINQ to XML
• Without LINQ to XML, creating XML content
  required multiple steps
   XmlDocument creates new elements
   Set properties
   Append as child of new parent
• See DemoNoLinqToXml




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment
   All inherit from XObject




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment
   All inherit from XObject
• Each provides overloaded constructors




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment
   All inherit from XObject
• Each provides overloaded constructors
   You pass as much information as necessary to
    generate the XML




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment
   All inherit from XObject
• Each provides overloaded constructors
   You pass as much information as necessary to
    generate the XML
   More readable than XmlDocument code




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using System.Xml.Linq Classes
• System.Xml.Linq namespace adds classes
   XElement, XAttribute, XComment
   All inherit from XObject
• Each provides overloaded constructors
   You pass as much information as necessary to
    generate the XML
   More readable than XmlDocument code
• Examine CreateXmlContent



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating Data: From and Select




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Creating Data: From and Select
• From clause sets up range variable iterating
  over data source




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating Data: From and Select
• From clause sets up range variable iterating
  over data source
• Select clause specifies "shape" of resulting data




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating Data: From and Select
• From clause sets up range variable iterating
  over data source
• Select clause specifies "shape" of resulting data
• Take advantage of these facts to use LINQ to
  generate XML content




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating Data: From and Select
• From clause sets up range variable iterating
  over data source
• Select clause specifies "shape" of resulting data
• Take advantage of these facts to use LINQ to
  generate XML content
• Examine DemoLinqToXml




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Visual Basic Only (VB)




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Visual Basic Only (VB)
• Only Visual Basic adds new features that make
  it far easier to work with LINQ to XML




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Visual Basic Only (VB)
• Only Visual Basic adds new features that make
  it far easier to work with LINQ to XML
   XML literals




              Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Visual Basic Only (VB)
• Only Visual Basic adds new features that make
  it far easier to work with LINQ to XML
   XML literals
   XML replacement tokens




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Visual Basic Only (VB)
• Only Visual Basic adds new features that make
  it far easier to work with LINQ to XML
   XML literals
   XML replacement tokens
• See CreateXmlContentVBOnly




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Learn More!




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
Learn More!
• This is an excerpt from a larger course. Visit
  www.learnnowonline.com for the full details!




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company

Contenu connexe

Tendances

Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQDoncho Minkov
 
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 Overviewpradeepkothiyal
 
Apollo Server III
Apollo Server IIIApollo Server III
Apollo Server IIINodeXperts
 
Apollo Server IV
Apollo Server IVApollo Server IV
Apollo Server IVNodeXperts
 
Apollo server II
Apollo server IIApollo server II
Apollo server IINodeXperts
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
Whats new in .NET for 2019
Whats new in .NET for 2019Whats new in .NET for 2019
Whats new in .NET for 2019Rory Preddy
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of RESTYos Riady
 
Introducing Arc: A Common Intermediate Language for Unified Batch and Stream...
Introducing Arc:  A Common Intermediate Language for Unified Batch and Stream...Introducing Arc:  A Common Intermediate Language for Unified Batch and Stream...
Introducing Arc: A Common Intermediate Language for Unified Batch and Stream...Flink Forward
 
Easy Dataweave transformations - Ashutosh
Easy Dataweave transformations - AshutoshEasy Dataweave transformations - Ashutosh
Easy Dataweave transformations - AshutoshStrawhatLuffy11
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDatabricks
 
New in Spring Framework 5.0: Functional Web Framework
New in Spring Framework 5.0: Functional Web FrameworkNew in Spring Framework 5.0: Functional Web Framework
New in Spring Framework 5.0: Functional Web FrameworkVMware Tanzu
 

Tendances (20)

Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
 
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
 
Linq in asp.net
Linq in asp.netLinq in asp.net
Linq in asp.net
 
Linq to sql
Linq to sqlLinq to sql
Linq to sql
 
Apollo Server
Apollo ServerApollo Server
Apollo Server
 
Apollo Server III
Apollo Server IIIApollo Server III
Apollo Server III
 
Apollo Server IV
Apollo Server IVApollo Server IV
Apollo Server IV
 
Apollo server II
Apollo server IIApollo server II
Apollo server II
 
GraphQL 101
GraphQL 101GraphQL 101
GraphQL 101
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
Tolog Updates
Tolog UpdatesTolog Updates
Tolog Updates
 
Whats new in .NET for 2019
Whats new in .NET for 2019Whats new in .NET for 2019
Whats new in .NET for 2019
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of REST
 
GraphQL
GraphQLGraphQL
GraphQL
 
Introducing Arc: A Common Intermediate Language for Unified Batch and Stream...
Introducing Arc:  A Common Intermediate Language for Unified Batch and Stream...Introducing Arc:  A Common Intermediate Language for Unified Batch and Stream...
Introducing Arc: A Common Intermediate Language for Unified Batch and Stream...
 
Easy Dataweave transformations - Ashutosh
Easy Dataweave transformations - AshutoshEasy Dataweave transformations - Ashutosh
Easy Dataweave transformations - Ashutosh
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark Applications
 
New in Spring Framework 5.0: Functional Web Framework
New in Spring Framework 5.0: Functional Web FrameworkNew in Spring Framework 5.0: Functional Web Framework
New in Spring Framework 5.0: Functional Web Framework
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 

En vedette

Introduccion a LINQ
Introduccion a LINQIntroduccion a LINQ
Introduccion a LINQTonymx
 
C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesSami Mut
 
OPC Unified Architecture
OPC Unified ArchitectureOPC Unified Architecture
OPC Unified ArchitectureVishwa Mohan
 
Satyapriya rajguru: Every day, in one way or another.
Satyapriya  rajguru:  Every day, in one way or another.Satyapriya  rajguru:  Every day, in one way or another.
Satyapriya rajguru: Every day, in one way or another.Satyapriya Rajguru
 

En vedette (8)

Introduccion a LINQ
Introduccion a LINQIntroduccion a LINQ
Introduccion a LINQ
 
jQuery
jQueryjQuery
jQuery
 
Linq
LinqLinq
Linq
 
LINQ and LINQPad
LINQ and LINQPadLINQ and LINQPad
LINQ and LINQPad
 
C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slides
 
5 g Technology
5 g  Technology5 g  Technology
5 g Technology
 
OPC Unified Architecture
OPC Unified ArchitectureOPC Unified Architecture
OPC Unified Architecture
 
Satyapriya rajguru: Every day, in one way or another.
Satyapriya  rajguru:  Every day, in one way or another.Satyapriya  rajguru:  Every day, in one way or another.
Satyapriya rajguru: Every day, in one way or another.
 

Similaire à Introducing LINQ

Using The .NET Framework
Using The .NET FrameworkUsing The .NET Framework
Using The .NET FrameworkLearnNowOnline
 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity FrameworkLearnNowOnline
 
WPF: Working with Data
WPF: Working with DataWPF: Working with Data
WPF: Working with DataLearnNowOnline
 
Progetta, crea e gestisci Modern Application per web e mobile su AWS
Progetta, crea e gestisci Modern Application per web e mobile su AWSProgetta, crea e gestisci Modern Application per web e mobile su AWS
Progetta, crea e gestisci Modern Application per web e mobile su AWSAmazon Web Services
 
What's new in Silverlight 5
What's new in Silverlight 5What's new in Silverlight 5
What's new in Silverlight 5LearnNowOnline
 
Real Time and Offline Applications with GraphQL
Real Time and Offline Applications with GraphQLReal Time and Offline Applications with GraphQL
Real Time and Offline Applications with GraphQLAmazon Web Services
 
Creating a World-Class RESTful Web Services API
Creating a World-Class RESTful Web Services APICreating a World-Class RESTful Web Services API
Creating a World-Class RESTful Web Services APIDavid Keener
 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsLearnNowOnline
 
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...Amazon Web Services
 
MBL306_Mobile State of the Union
MBL306_Mobile State of the UnionMBL306_Mobile State of the Union
MBL306_Mobile State of the UnionAmazon Web Services
 
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5LearnNowOnline
 
Solving Real World Challenges with Enterprise Search
Solving Real World Challenges with Enterprise SearchSolving Real World Challenges with Enterprise Search
Solving Real World Challenges with Enterprise SearchAgnes Molnar
 
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdf
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdfDEV305_Manage Your Applications with AWS Elastic Beanstalk.pdf
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdfAmazon Web Services
 
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...Amazon Web Services
 
Use Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemUse Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemAmazon Web Services
 
Use Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemUse Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemAmazon Web Services
 

Similaire à Introducing LINQ (20)

Using The .NET Framework
Using The .NET FrameworkUsing The .NET Framework
Using The .NET Framework
 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity Framework
 
WPF Binding
WPF BindingWPF Binding
WPF Binding
 
WPF: Working with Data
WPF: Working with DataWPF: Working with Data
WPF: Working with Data
 
Progetta, crea e gestisci Modern Application per web e mobile su AWS
Progetta, crea e gestisci Modern Application per web e mobile su AWSProgetta, crea e gestisci Modern Application per web e mobile su AWS
Progetta, crea e gestisci Modern Application per web e mobile su AWS
 
What's new in Silverlight 5
What's new in Silverlight 5What's new in Silverlight 5
What's new in Silverlight 5
 
Real Time and Offline Applications with GraphQL
Real Time and Offline Applications with GraphQLReal Time and Offline Applications with GraphQL
Real Time and Offline Applications with GraphQL
 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
 
Creating a World-Class RESTful Web Services API
Creating a World-Class RESTful Web Services APICreating a World-Class RESTful Web Services API
Creating a World-Class RESTful Web Services API
 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and Expressions
 
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...
Build secure, offline, real-time-enabled mobile apps - MAD304 - Atlanta AWS S...
 
React Native Workshop
React Native WorkshopReact Native Workshop
React Native Workshop
 
MBL306_Mobile State of the Union
MBL306_Mobile State of the UnionMBL306_Mobile State of the Union
MBL306_Mobile State of the Union
 
The Entity Data Model
The Entity Data ModelThe Entity Data Model
The Entity Data Model
 
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5Building Windows 8 Metro Style Applications Using JavaScript and HTML5
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
 
Solving Real World Challenges with Enterprise Search
Solving Real World Challenges with Enterprise SearchSolving Real World Challenges with Enterprise Search
Solving Real World Challenges with Enterprise Search
 
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdf
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdfDEV305_Manage Your Applications with AWS Elastic Beanstalk.pdf
DEV305_Manage Your Applications with AWS Elastic Beanstalk.pdf
 
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...
Ripping off the Bandage: Re-Architecting Traditional Three-Tier Monoliths to ...
 
Use Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemUse Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition System
 
Use Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition SystemUse Amazon Rekognition to Build a Facial Recognition System
Use Amazon Rekognition to Build a Facial Recognition System
 

Plus de LearnNowOnline

Windows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesWindows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesLearnNowOnline
 
SQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionSQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionLearnNowOnline
 
New in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDENew in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDELearnNowOnline
 
Attributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingAttributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingLearnNowOnline
 
Asynchronous Programming
Asynchronous ProgrammingAsynchronous Programming
Asynchronous ProgrammingLearnNowOnline
 
Object oriented techniques
Object oriented techniquesObject oriented techniques
Object oriented techniquesLearnNowOnline
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScriptLearnNowOnline
 
SharePoint Document Management
SharePoint Document ManagementSharePoint Document Management
SharePoint Document ManagementLearnNowOnline
 
SharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathSharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathLearnNowOnline
 
Managing site collections
Managing site collectionsManaging site collections
Managing site collectionsLearnNowOnline
 
Sql 2012 development and programming
Sql 2012  development and programmingSql 2012  development and programming
Sql 2012 development and programmingLearnNowOnline
 
KnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCKnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCLearnNowOnline
 
Expression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignExpression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignLearnNowOnline
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVCLearnNowOnline
 
Working with Controllers and Actions in MVC
Working with Controllers and Actions in MVCWorking with Controllers and Actions in MVC
Working with Controllers and Actions in MVCLearnNowOnline
 
Creating a User Interface
Creating a User InterfaceCreating a User Interface
Creating a User InterfaceLearnNowOnline
 

Plus de LearnNowOnline (20)

Windows 8: Shapes and Geometries
Windows 8: Shapes and GeometriesWindows 8: Shapes and Geometries
Windows 8: Shapes and Geometries
 
SQL: Permissions and Data Protection
SQL: Permissions and Data ProtectionSQL: Permissions and Data Protection
SQL: Permissions and Data Protection
 
New in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDENew in the Visual Studio 2012 IDE
New in the Visual Studio 2012 IDE
 
Attributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programmingAttributes, reflection, and dynamic programming
Attributes, reflection, and dynamic programming
 
Asynchronous Programming
Asynchronous ProgrammingAsynchronous Programming
Asynchronous Programming
 
A tour of SQL Server
A tour of SQL ServerA tour of SQL Server
A tour of SQL Server
 
Generics
GenericsGenerics
Generics
 
Object oriented techniques
Object oriented techniquesObject oriented techniques
Object oriented techniques
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
 
SharePoint Document Management
SharePoint Document ManagementSharePoint Document Management
SharePoint Document Management
 
SharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPathSharePoint: Introduction to InfoPath
SharePoint: Introduction to InfoPath
 
Managing site collections
Managing site collectionsManaging site collections
Managing site collections
 
Web API HTTP Pipeline
Web API HTTP PipelineWeb API HTTP Pipeline
Web API HTTP Pipeline
 
SQL Server: Security
SQL Server: SecuritySQL Server: Security
SQL Server: Security
 
Sql 2012 development and programming
Sql 2012  development and programmingSql 2012  development and programming
Sql 2012 development and programming
 
KnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVCKnockOutJS with ASP.NET MVC
KnockOutJS with ASP.NET MVC
 
Expression Blend Motion & Interaction Design
Expression Blend Motion & Interaction DesignExpression Blend Motion & Interaction Design
Expression Blend Motion & Interaction Design
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Working with Controllers and Actions in MVC
Working with Controllers and Actions in MVCWorking with Controllers and Actions in MVC
Working with Controllers and Actions in MVC
 
Creating a User Interface
Creating a User InterfaceCreating a User Interface
Creating a User Interface
 

Dernier

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Introducing LINQ

  • 1. Introducing LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 2. Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 3. Objectives • Motivate the need for LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 4. Objectives • Motivate the need for LINQ • Learn about the various LINQ providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 5. Objectives • Motivate the need for LINQ • Learn about the various LINQ providers • Investigate simple LINQ to Objects, LINQ to SQL, and LINQ to XML samples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 6. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 7. Agenda • Language Integrated Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 8. Agenda • Language Integrated Query • Structure of a LINQ Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 9. Agenda • Language Integrated Query • Structure of a LINQ Query • Some LINQ Examples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 10. Language Integrated Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 11. Language Integrated Query • Querying and manipulating data has always been a fundamental part of our jobs as developers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 12. Language Integrated Query • Querying and manipulating data has always been a fundamental part of our jobs as developers • Data formats change, but core needs are the same Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 13. Language Integrated Query • Querying and manipulating data has always been a fundamental part of our jobs as developers • Data formats change, but core needs are the same  Must create, retrieve, update, and delete data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 14. Data Access (DBASE, 1985 or so) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 15. Data Access (DBASE, 1985 or so) USE empl REPLACE ALL salary WITH (salary * 1.1) FOR supervises > 0 LIST ALL fname, lname, salary FOR Supervises > 0 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 16. Data Access APIs (C#) (late 1990s/early 2000s) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 17. Data Access APIs (C#) (late 1990s/early 2000s) SqlCommand cmd = new SqlCommand( @"SELECT fname, lname, salary FROM Empl WHERE supervises > @p0" ); cmd.Parameters.AddWithValue("@po", 0); SqlConnection c = new SqlConnection(…); c.Open(); DataReader people = c.Execute(cmd); while (people.Read()) { string fname = (string) people["fname"]; string lname = (string) people["lname"]; double salary = (double) people["salary"]; } people.Close(); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 18. Object/Relational Mapping (C#) (last few years) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 19. Object/Relational Mapping (C#) (last few years) public class Employee { public string FirstName; public string LastName; public double Salary; } IList employees = session.CreateCriteria(typeof(Employee)) .Add(Expression.Gt("supervises", 0) .List(); foreach(Employee employee in employees) { string fname = employee.FirstName; string lname = employee.LastName; double salary = employee.Salary; } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 20. Data Access APIs (VB) (late 1990s/early 2000s) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 21. Data Access APIs (VB) (late 1990s/early 2000s) Dim cmd As New SqlCommand( _ "SELECT fname, lname, salary" & _ " FROM Empl" & _ " WHERE supervises > @p0") cmd.Parameters.AddWithValue("@p0", 0) Dim cnn as New SqlConnection(…) cnn.Open() Dim people As DataReader = cnn.Execute(cmd) While people.Read() Dim fname As String = people("fname").ToString() Dim lname As String = people("lname").ToString() Dim salary As Double = CDbl(people("salary")) End While people.Close(); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 22. Object/Relational Mapping (VB) (last few years) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 23. Object/Relational Mapping (VB) (last few years) Public Class Employee Public FirstName As String Public LastName As String Public Salary As Double End Class Dim employees As IList = _ session.CreateCriteria(GetType(Employee). _ Add(Expression.Gt("supervises", 0).List() For Each employee As Employee In Employees Dim fname As String = people("fname").ToString() Dim lname As String = people("lname").ToString() Dim salary As Double = CDbl(people("salary")) Next employee Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 24. Object/Relational Mapping Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 25. Object/Relational Mapping • Provides: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 26. Object/Relational Mapping • Provides:  Mapping between relational data to/from objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 27. Object/Relational Mapping • Provides:  Mapping between relational data to/from objects  Cleaner integration of business rules and validation Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 28. LINQ to the Rescue Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 29. LINQ to the Rescue • How do you retrieve non-relational data? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 30. LINQ to the Rescue • How do you retrieve non-relational data?  XML, RSS, Web Services, REST, AD, Files, and so on Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 31. LINQ to the Rescue • How do you retrieve non-relational data?  XML, RSS, Web Services, REST, AD, Files, and so on • How do you interact with plain old objects? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 32. LINQ to the Rescue • How do you retrieve non-relational data?  XML, RSS, Web Services, REST, AD, Files, and so on • How do you interact with plain old objects?  How do you interact and query custom domain models? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 33. LINQ to the Rescue • How do you retrieve non-relational data?  XML, RSS, Web Services, REST, AD, Files, and so on • How do you interact with plain old objects?  How do you interact and query custom domain models? • How do you enable clean code in both a strongly typed and dynamic language world? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 34. LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 35. LINQ • Query, Set, and Transform Operations for .NET Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 36. LINQ • Query, Set, and Transform Operations for .NET • Makes querying data a core programming concept Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 37. LINQ • Query, Set, and Transform Operations for .NET • Makes querying data a core programming concept • Works with all types and shapes of data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 38. LINQ • Query, Set, and Transform Operations for .NET • Makes querying data a core programming concept • Works with all types and shapes of data  Relational databases Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 39. LINQ • Query, Set, and Transform Operations for .NET • Makes querying data a core programming concept • Works with all types and shapes of data  Relational databases  XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 40. LINQ • Query, Set, and Transform Operations for .NET • Makes querying data a core programming concept • Works with all types and shapes of data  Relational databases  XML  Plain old objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 41. LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 42. LINQ • Works with all .NET languages Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 43. LINQ • Works with all .NET languages  VB and C# have integrated language support Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 44. LINQ • Works with all .NET languages  VB and C# have integrated language support • Works in any kind of project including Windows and Web projects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 45. LINQ • Works with all .NET languages  VB and C# have integrated language support • Works in any kind of project including Windows and Web projects • Also can find third party LINQ providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 46. LINQ • Works with all .NET languages  VB and C# have integrated language support • Works in any kind of project including Windows and Web projects • Also can find third party LINQ providers  Amazon.com for example Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 47. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 48. LINQ Providers • C# uses LINQ providers to map your LINQ queries to the data source that you’re querying Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 49. LINQ Providers • C# uses LINQ providers to map your LINQ queries to the data source that you’re querying • The LINQ provider takes the query that you create in code, and converts it into commands that the data source will be able to execute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 50. LINQ Providers • C# uses LINQ providers to map your LINQ queries to the data source that you’re querying • The LINQ provider takes the query that you create in code, and converts it into commands that the data source will be able to execute • On return from executing the commands, the provider also converts the data into objects that create your query results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 51. LINQ Overview Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 52. LINQ Overview LINQ enabled data sources Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 53. LINQ Overview LINQ enabled data sources LINQ To Objects Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 54. LINQ Overview LINQ enabled data sources LINQ enabled ADO.NET LINQ LINQ LINQ LINQ To Objects To Datasets To SQL To Entities Objects Relational Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 55. LINQ Overview 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 56. LINQ Overview 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 57. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 58. LINQ Providers • LINQ to Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 59. LINQ Providers • LINQ to Objects  Allows you to query in-memory of sets of objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 60. LINQ Providers • LINQ to Objects  Allows you to query in-memory of sets of objects o Collections, arrays, and lists Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 61. LINQ Providers • LINQ to Objects  Allows you to query in-memory of sets of objects o Collections, arrays, and lists o If a class implements IEnumerable (or generic version), you can use LINQ to query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 62. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 63. LINQ Providers • LINQ to SQL Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 64. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 65. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 66. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source  Use System.Data.Linq.DataContext class to provide the “plumbing” Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 67. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source  Use System.Data.Linq.DataContext class to provide the “plumbing”  Can use Visual Studio’s O/R designer to make it easy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 68. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source  Use System.Data.Linq.DataContext class to provide the “plumbing”  Can use Visual Studio’s O/R designer to make it easy o For now, will mark up entity class manually Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 69. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source  Use System.Data.Linq.DataContext class to provide the “plumbing”  Can use Visual Studio’s O/R designer to make it easy o For now, will mark up entity class manually  DataContext manages communication Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 70. LINQ Providers • LINQ to SQL  Allows you to query and modify data in SQL Server  You provide mapping between a modeling class and schema in data source  Use System.Data.Linq.DataContext class to provide the “plumbing”  Can use Visual Studio’s O/R designer to make it easy o For now, will mark up entity class manually  DataContext manages communication o Stores state for optimistic concurrency checks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 71. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 72. LINQ Providers • LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 73. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 74. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content  Can work with XML from file, or from stream Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 75. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content  Can work with XML from file, or from stream  System.Linq.Xml.XObject, Xnode, XElement, XAttribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 76. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content  Can work with XML from file, or from stream  System.Linq.Xml.XObject, Xnode, XElement, XAttribute o Make it easy to work with XML programmatically Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 77. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content  Can work with XML from file, or from stream  System.Linq.Xml.XObject, Xnode, XElement, XAttribute o Make it easy to work with XML programmatically  LINQ to XML provider works with these classes to create queryable collections of XML data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 78. LINQ Providers • LINQ to XML  Easy to query, modify, and generate XML content  Can work with XML from file, or from stream  System.Linq.Xml.XObject, Xnode, XElement, XAttribute o Make it easy to work with XML programmatically  LINQ to XML provider works with these classes to create queryable collections of XML data  Visual Basic adds language-specific rich support for LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 79. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 80. LINQ Providers • LINQ to DataSet Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 81. LINQ Providers • LINQ to DataSet  Allows you to query and update data in ADO.NET dataset Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 82. LINQ Providers • LINQ to DataSet  Allows you to query and update data in ADO.NET dataset  Can use querying engine against data already cached in client application Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 83. LINQ Providers • LINQ to DataSet  Allows you to query and update data in ADO.NET dataset  Can use querying engine against data already cached in client application  Strongly typed datasets easier to work with than standard datasets Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 84. LINQ Providers • LINQ to DataSet  Allows you to query and update data in ADO.NET dataset  Can use querying engine against data already cached in client application  Strongly typed datasets easier to work with than standard datasets o You already know the schema—it's just LINQ to Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 85. LINQ Providers • LINQ to DataSet  Allows you to query and update data in ADO.NET dataset  Can use querying engine against data already cached in client application  Strongly typed datasets easier to work with than standard datasets o You already know the schema—it's just LINQ to Objects  Language features make it possible to query standard datasets Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 86. LINQ Providers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 87. LINQ Providers • LINQ to Entities Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 88. LINQ Providers • LINQ to Entities  Allows developers to query data exposed using Entity Data Model (EDM) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 89. LINQ Providers • LINQ to Entities  Allows developers to query data exposed using Entity Data Model (EDM) o EDM: conceptual model, allows applications to interact with data as if it was a collection of objects, or entities Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 90. LINQ Providers • LINQ to Entities  Allows developers to query data exposed using Entity Data Model (EDM) o EDM: conceptual model, allows applications to interact with data as if it was a collection of objects, or entities  Using EDM, ADO.NET exposes entities as objects in .NET Framework, allows LINQ queries to operate Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 91. LINQ Providers • LINQ to Entities  Allows developers to query data exposed using Entity Data Model (EDM) o EDM: conceptual model, allows applications to interact with data as if it was a collection of objects, or entities  Using EDM, ADO.NET exposes entities as objects in .NET Framework, allows LINQ queries to operate  Conceptually, same as LINQ to SQL, but works with any data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 92. LINQ Providers • LINQ to Entities  Allows developers to query data exposed using Entity Data Model (EDM) o EDM: conceptual model, allows applications to interact with data as if it was a collection of objects, or entities  Using EDM, ADO.NET exposes entities as objects in .NET Framework, allows LINQ queries to operate  Conceptually, same as LINQ to SQL, but works with any data source o Doesn't assume one-to-one mapping between classes and schema Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 93. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 94. Agenda • Language Integrated Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 95. Agenda • Language Integrated Query • Structure of a LINQ Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 96. Agenda • Language Integrated Query • Structure of a LINQ Query • Some LINQ Examples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 97. Structure of a LINQ Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 98. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 99. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 100. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 101. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering  Sorting Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 102. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering  Sorting  Grouping Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 103. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering  Sorting  Grouping  Joining Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 104. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering  Sorting  Grouping  Joining  Calculating Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 105. Structure of a LINQ Query • LINQ query contains clauses that specify data sources and iteration variables • Query expressions can also include:  Filtering  Sorting  Grouping  Joining  Calculating • From required—indicates source of data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 106. The Three Stages of a LINQ Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 107. The Three Stages of a LINQ Query • Each LINQ query requires three stages: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 108. The Three Stages of a LINQ Query • Each LINQ query requires three stages:  Obtain the data source(s) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 109. The Three Stages of a LINQ Query • Each LINQ query requires three stages:  Obtain the data source(s)  Create the query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 110. The Three Stages of a LINQ Query • Each LINQ query requires three stages:  Obtain the data source(s)  Create the query  Execute the query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 111. The Data Source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 112. The Data Source • In the sample, data source is an array of integers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 113. The Data Source • In the sample, data source is an array of integers • Because array implements IEnumerable, can query against it Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 114. The Data Source • In the sample, data source is an array of integers • Because array implements IEnumerable, can query against it  Treated as a "queryable" type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 115. XML as Data Source (VB) ' VB Dim items As XElement = XElement.Load("C:Grocery.xml") Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 116. XML as Data Source (VB) • Could use LINQ to XML to load contents of an XML file into a queryable collection of XElement instances: ' VB Dim items As XElement = XElement.Load("C:Grocery.xml") Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 117. XML as Data Source (C#) // C#: XElement items = XElement.Load(@"C:Grocery.xml"); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 118. XML as Data Source (C#) • Could use LINQ to XML to load contents of an XML file into a queryable collection of XElement instances: // C#: XElement items = XElement.Load(@"C:Grocery.xml"); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 119. SQL as Data Source (VB) ' VB Dim dc As New DataContext( _ My.Settings.NorthwindConnectionString) Dim customers As Table(Of Customer) = _ dc.GetTable(Of Customer) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 120. SQL as Data Source (VB) • Can use LINQ to SQL once you have a class that provides object-relational mapping: ' VB Dim dc As New DataContext( _ My.Settings.NorthwindConnectionString) Dim customers As Table(Of Customer) = _ dc.GetTable(Of Customer) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 121. SQL as Data Source (C#) // C# DataContext dc = new DataContext( Properties.Settings.Default.NorthwindConnectionString); Table<Customer> customers = dc.GetTable<Customer>(); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 122. SQL as Data Source (C#) • Can use LINQ to SQL once you have a class that provides object-relational mapping: // C# DataContext dc = new DataContext( Properties.Settings.Default.NorthwindConnectionString); Table<Customer> customers = dc.GetTable<Customer>(); Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 123. The Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 124. The Query • Given the data source, can specify the query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 125. The Query • Given the data source, can specify the query  Defines the information you want to retrieve Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 126. The Query • Given the data source, can specify the query  Defines the information you want to retrieve  Optionally, can specify how to sort, filter, and group Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 127. The Query • Given the data source, can specify the query  Defines the information you want to retrieve  Optionally, can specify how to sort, filter, and group • LINQ queries are generally the same, no matter the data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 128. The Query Execution Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 129. The Query Execution • Query definition doesn't execute the query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 130. The Query Execution • Query definition doesn't execute the query • Instead, defines the query that will run when it's time to retrieve the data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 131. The Query Execution • Query definition doesn't execute the query • Instead, defines the query that will run when it's time to retrieve the data • To execute, need to run code that forces query reference to require the values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 132. The Query Execution • Query definition doesn't execute the query • Instead, defines the query that will run when it's time to retrieve the data • To execute, need to run code that forces query reference to require the values  In this course, For Each loop generally forces Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 133. Returning Multiple Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 134. Returning Multiple Values • Want to return multiple values? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 135. Returning Multiple Values • Want to return multiple values? • In general, two options: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 136. Returning Multiple Values • Want to return multiple values? • In general, two options:  Create class that exposes properties Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 137. Returning Multiple Values • Want to return multiple values? • In general, two options:  Create class that exposes properties o Specify new instance of class in Select Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 138. Returning Multiple Values • Want to return multiple values? • In general, two options:  Create class that exposes properties o Specify new instance of class in Select  Specify values in Select clause Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 139. Returning Multiple Values • Want to return multiple values? • In general, two options:  Create class that exposes properties o Specify new instance of class in Select  Specify values in Select clause o Compiler creates a new anonymous type containing just the properties you specify Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 140. Creating a Class for Return Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 141. Creating a Class for Return Values • See MyFileInfo class and MultipleReturnValues1 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 142. Using an Anonymous Type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 143. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 144. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 145. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query  Why create a class simply to return values? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 146. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query  Why create a class simply to return values? • Anonymous types generally simpler Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 147. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query  Why create a class simply to return values? • Anonymous types generally simpler • Compiler creates the type, based on values specified in query Select clause Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 148. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query  Why create a class simply to return values? • Anonymous types generally simpler • Compiler creates the type, based on values specified in query Select clause  You don't know the type's name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 149. Using an Anonymous Type • In previous example, code created a simple type (MyFileInfo)  Type contained properties returned by query  Why create a class simply to return values? • Anonymous types generally simpler • Compiler creates the type, based on values specified in query Select clause  You don't know the type's name  Implicit type definition makes it possible to work with the anonymous type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 150. Deferred Execution Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 151. Deferred Execution • Defining query doesn't cause query to execute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 152. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 153. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 154. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words:  Creating query doesn't return data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 155. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words:  Creating query doesn't return data  Provides potential for returning data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 156. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words:  Creating query doesn't return data  Provides potential for returning data • Can take advantage of deferred execution Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 157. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words:  Creating query doesn't return data  Provides potential for returning data • Can take advantage of deferred execution  Break query into small steps without fear of multiple trips to the server for data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 158. Deferred Execution • Defining query doesn't cause query to execute • Must actually force execution by requesting data • In other words:  Creating query doesn't return data  Provides potential for returning data • Can take advantage of deferred execution  Break query into small steps without fear of multiple trips to the server for data  Makes debugging simpler, as well Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 159. Converting Results to Force Execution Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 160. Converting Results to Force Execution • Sometimes, might want to immediately execute query and cache its results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 161. Converting Results to Force Execution • Sometimes, might want to immediately execute query and cache its results • Can use method like ToList or ToArray Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 162. Converting Results to Force Execution • Sometimes, might want to immediately execute query and cache its results • Can use method like ToList or ToArray • Each executes query, and returns results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 163. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 164. Agenda • Language Integrated Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 165. Agenda • Language Integrated Query • Structure of a LINQ Query Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 166. Agenda • Language Integrated Query • Structure of a LINQ Query • Some LINQ Examples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 167. Some LINQ Examples Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 168. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 169. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ • Introduce some of the new language features for LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 170. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ • Introduce some of the new language features for LINQ • Examples of: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 171. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ • Introduce some of the new language features for LINQ • Examples of:  LINQ to Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 172. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ • Introduce some of the new language features for LINQ • Examples of:  LINQ to Objects  LINQ to SQL Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 173. Some LINQ Examples • Get a taste of the kinds of things you can do with LINQ • Introduce some of the new language features for LINQ • Examples of:  LINQ to Objects  LINQ to SQL  LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 174. LINQ to Objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 175. LINQ to Objects • Can use any collection that implements IEnumerable (or generic version) as data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 176. LINQ to Objects • Can use any collection that implements IEnumerable (or generic version) as data source • Note example uses multiple overloaded versions of DisplayResults procedure Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 177. Query Syntax/Extension Methods (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 178. Query Syntax/Extension Methods (C#) • How does language compiler turn keywords like select, where, and orderby into executable code? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 179. Query Syntax/Extension Methods (C#) • How does language compiler turn keywords like select, where, and orderby into executable code?  Keywords need to be converted into methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 180. Query Syntax/Extension Methods (C#) • How does language compiler turn keywords like select, where, and orderby into executable code?  Keywords need to be converted into methods • Each LINQ keyword corresponds to a single method of the System.Linq.Enumerable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 181. Query Syntax/Extension Methods (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 182. Query Syntax/Extension Methods (VB) • How does language compiler turn keywords like Select, Where, and Order By into executable code? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 183. Query Syntax/Extension Methods (VB) • How does language compiler turn keywords like Select, Where, and Order By into executable code?  Keywords need to be converted into methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 184. Query Syntax/Extension Methods (VB) • How does language compiler turn keywords like Select, Where, and Order By into executable code?  Keywords need to be converted into methods • Each LINQ keyword corresponds to a single method of the System.Linq.Enumerable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 185. Continuing the Investigation Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 186. Continuing the Investigation • So compiler converts keywords into methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 187. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 188. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 189. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on  Where do those methods come from? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 190. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on  Where do those methods come from? • LINQ requires ability to extend existing interfaces/classes without changing their definitions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 191. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on  Where do those methods come from? • LINQ requires ability to extend existing interfaces/classes without changing their definitions  Available because of new extension methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 192. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on  Where do those methods come from? • LINQ requires ability to extend existing interfaces/classes without changing their definitions  Available because of new extension methods • Extension method exists in one class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 193. Continuing the Investigation • So compiler converts keywords into methods • But IEnumerable is an old interface  It doesn't support Where, Select, and so on  Where do those methods come from? • LINQ requires ability to extend existing interfaces/classes without changing their definitions  Available because of new extension methods • Extension method exists in one class  Allows code to extend a different class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 194. Extension Methods (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 195. Extension Methods (VB) • Add new methods to existing classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 196. Extension Methods (VB) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 197. Extension Methods (VB) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 198. Extension Methods (VB) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 199. Extension Methods (VB) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending  Exist within a module Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 200. Extension Methods (VB) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending  Exist within a module  Include the <Extension()> attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 201. Extension Methods (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 202. Extension Methods (C#) • Add new methods to existing classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 203. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 204. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 205. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 206. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending  Exist within a static class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 207. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending  Exist within a static class  Be a static method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 208. Extension Methods (C#) • Add new methods to existing classes • Not just for LINQ, but crucial for LINQ • Extension methods must:  Accept a parameter, the type of which defines the class that you’re extending  Exist within a static class  Be a static method  Include the this keyword in the parameter Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 209. Using LINQ to Group Output Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 210. Using LINQ to Group Output • System.Linq.Enumerable class provides large number of methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 211. Using LINQ to Group Output • System.Linq.Enumerable class provides large number of methods • Look for grouping methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 212. LINQ to SQL Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 213. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 214. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 215. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 216. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties • To support LINQ to SQL, must add custom .NET attributes to class that represents the schema Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 217. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties • To support LINQ to SQL, must add custom .NET attributes to class that represents the schema • Map a class to a table Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 218. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties • To support LINQ to SQL, must add custom .NET attributes to class that represents the schema • Map a class to a table  Map properties within the class to columns Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 219. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties • To support LINQ to SQL, must add custom .NET attributes to class that represents the schema • Map a class to a table  Map properties within the class to columns  All by adding attributes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 220. LINQ to SQL • Allows standard LINQ queries to work with data stored in relational databases  Extends ADO.NET  Adds support for mapping tables and rows to classes and properties • To support LINQ to SQL, must add custom .NET attributes to class that represents the schema • Map a class to a table  Map properties within the class to columns  All by adding attributes • Can represent relationships as well Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 221. Linking a Class to SQL Server Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 222. Linking a Class to SQL Server • Retrieve data from Northwind Customers table Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 223. Linking a Class to SQL Server • Retrieve data from Northwind Customers table • Retrieve CustomerID, CompanyName, ContactName, Country, Region Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 224. Linking a Class to SQL Server • Retrieve data from Northwind Customers table • Retrieve CustomerID, CompanyName, ContactName, Country, Region • Indicate primary key, columns, and rename column Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 225. Creating the Class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 226. Creating the Class • Start by creating a class with properties that correspond to columns in the database Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 227. Creating the Class • Start by creating a class with properties that correspond to columns in the database • See Customer1 class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 228. Marking Up the Class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 229. Marking Up the Class • Use attributes from the System.Data.Linq.Mapping namespace Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 230. Marking Up the Class • Use attributes from the System.Data.Linq.Mapping namespace  Table Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 231. Marking Up the Class • Use attributes from the System.Data.Linq.Mapping namespace  Table  Column Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 232. Marking Up the Class • Use attributes from the System.Data.Linq.Mapping namespace  Table  Column  Many others Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 233. Marking Up the Class • Use attributes from the System.Data.Linq.Mapping namespace  Table  Column  Many others • See Customer class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 234. Retrieving a Collection of Customers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 235. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 236. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 237. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data  Need some class that converts requests for data into SQL strings, and executes/virtualizes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 238. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data  Need some class that converts requests for data into SQL strings, and executes/virtualizes • System.Data.Linq.DataContext class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 239. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data  Need some class that converts requests for data into SQL strings, and executes/virtualizes • System.Data.Linq.DataContext class  Includes members that provide access to data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 240. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data  Need some class that converts requests for data into SQL strings, and executes/virtualizes • System.Data.Linq.DataContext class  Includes members that provide access to data  Tracks changes to data for updates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 241. Retrieving a Collection of Customers • Need some mechanism to manage connection between class and data  Can't simply fill some huge data structure with all the data  Need some class that converts requests for data into SQL strings, and executes/virtualizes • System.Data.Linq.DataContext class  Includes members that provide access to data  Tracks changes to data for updates  O/R designer creates classes, and new class that inherits from DataContext Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 242. Determining the Actual SQL String Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 243. Determining the Actual SQL String • Set Log property of DataContext to stream Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 244. Determining the Actual SQL String • Set Log property of DataContext to stream  Sends all information to the output stream Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 245. Determining the Actual SQL String • Set Log property of DataContext to stream  Sends all information to the output stream • To simply see the SQL string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 246. Determining the Actual SQL String • Set Log property of DataContext to stream  Sends all information to the output stream • To simply see the SQL string  Call ToString method of query variable Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 247. Working with Anonymous Types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 248. Working with Anonymous Types • Anonymous types work well with LINQ to SQL Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 249. Working with Anonymous Types • Anonymous types work well with LINQ to SQL • Often need to retrieve just a subset of columns Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 250. Working with Anonymous Types • Anonymous types work well with LINQ to SQL • Often need to retrieve just a subset of columns  Or perform a calculation Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 251. Working with Anonymous Types • Anonymous types work well with LINQ to SQL • Often need to retrieve just a subset of columns  Or perform a calculation • See DemoLinqToSqlWithAnonymousTypes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 252. LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 253. LINQ to XML • Need to retrieve, create, query XML? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 254. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 255. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 256. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 257. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath • What if you could accomplish these using a standard syntax? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 258. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath • What if you could accomplish these using a standard syntax? • That is, what about LINQ? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 259. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath • What if you could accomplish these using a standard syntax? • That is, what about LINQ? • LINQ to XML makes it possible Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 260. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath • What if you could accomplish these using a standard syntax? • That is, what about LINQ? • LINQ to XML makes it possible • Examples create XML content Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 261. LINQ to XML • Need to retrieve, create, query XML? • .NET Framework provides rich support  Steps can be complex  Can require XSLT and XPath • What if you could accomplish these using a standard syntax? • That is, what about LINQ? • LINQ to XML makes it possible • Examples create XML content  Examine Course class, and CreateCourseList Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 262. Life without LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 263. Life without LINQ to XML • Without LINQ to XML, creating XML content required multiple steps Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 264. Life without LINQ to XML • Without LINQ to XML, creating XML content required multiple steps  XmlDocument creates new elements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 265. Life without LINQ to XML • Without LINQ to XML, creating XML content required multiple steps  XmlDocument creates new elements  Set properties Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 266. Life without LINQ to XML • Without LINQ to XML, creating XML content required multiple steps  XmlDocument creates new elements  Set properties  Append as child of new parent Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 267. Life without LINQ to XML • Without LINQ to XML, creating XML content required multiple steps  XmlDocument creates new elements  Set properties  Append as child of new parent • See DemoNoLinqToXml Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 268. Using System.Xml.Linq Classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 269. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 270. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 271. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment  All inherit from XObject Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 272. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment  All inherit from XObject • Each provides overloaded constructors Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 273. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment  All inherit from XObject • Each provides overloaded constructors  You pass as much information as necessary to generate the XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 274. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment  All inherit from XObject • Each provides overloaded constructors  You pass as much information as necessary to generate the XML  More readable than XmlDocument code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 275. Using System.Xml.Linq Classes • System.Xml.Linq namespace adds classes  XElement, XAttribute, XComment  All inherit from XObject • Each provides overloaded constructors  You pass as much information as necessary to generate the XML  More readable than XmlDocument code • Examine CreateXmlContent Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 276. Creating Data: From and Select Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 277. Creating Data: From and Select • From clause sets up range variable iterating over data source Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 278. Creating Data: From and Select • From clause sets up range variable iterating over data source • Select clause specifies "shape" of resulting data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 279. Creating Data: From and Select • From clause sets up range variable iterating over data source • Select clause specifies "shape" of resulting data • Take advantage of these facts to use LINQ to generate XML content Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 280. Creating Data: From and Select • From clause sets up range variable iterating over data source • Select clause specifies "shape" of resulting data • Take advantage of these facts to use LINQ to generate XML content • Examine DemoLinqToXml Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 281. Visual Basic Only (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 282. Visual Basic Only (VB) • Only Visual Basic adds new features that make it far easier to work with LINQ to XML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 283. Visual Basic Only (VB) • Only Visual Basic adds new features that make it far easier to work with LINQ to XML  XML literals Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 284. Visual Basic Only (VB) • Only Visual Basic adds new features that make it far easier to work with LINQ to XML  XML literals  XML replacement tokens Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 285. Visual Basic Only (VB) • Only Visual Basic adds new features that make it far easier to work with LINQ to XML  XML literals  XML replacement tokens • See CreateXmlContentVBOnly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 286. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 287. Learn More! • This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. DEMO: May want to investigate the sample, NumbersGreaterThan5\n
  86. DEMO: May want to investigate the sample, NumbersGreaterThan5\n
  87. DEMO: May want to investigate the sample, NumbersGreaterThan5\n
  88. DEMO: May want to investigate the sample, NumbersGreaterThan5\n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. DEMO: May want to investigate NumbersGreaterThan5 query\n
  97. DEMO: May want to investigate NumbersGreaterThan5 query\n
  98. DEMO: May want to investigate NumbersGreaterThan5 query\n
  99. DEMO: May want to investigate NumbersGreaterThan5 query\n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. \n
  107. \n
  108. \n
  109. \n
  110. \n
  111. DEMO: May want to work with anonymous type\n
  112. DEMO: May want to work with anonymous type\n
  113. DEMO: May want to work with anonymous type\n
  114. DEMO: May want to work with anonymous type\n
  115. DEMO: May want to work with anonymous type\n
  116. DEMO: May want to work with anonymous type\n
  117. DEMO: May want to work with anonymous type\n
  118. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  119. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  120. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  121. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  122. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  123. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  124. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  125. DEMO: May want to demo the following:\nDeferredExecution and DoubleIt procedures\nDeferredExecution1\n
  126. DEMO: May want to try ForcingExecution\n
  127. DEMO: May want to try ForcingExecution\n
  128. DEMO: May want to try ForcingExecution\n
  129. \n
  130. \n
  131. \n
  132. \n
  133. \n
  134. \n
  135. \n
  136. \n
  137. \n
  138. DEMO: May want to demo the following:\nNumberQuery0\nNumberQuery1\nNumberQueryTheOldWay\n
  139. DEMO: May want to demo the following:\nNumberQuery0\nNumberQuery1\nNumberQueryTheOldWay\n
  140. DEMO: May want to look in ILDASM for disassembled output\n
  141. DEMO: May want to look in ILDASM for disassembled output\n
  142. DEMO: May want to look in ILDASM for disassembled output\n
  143. DEMO: May want to look in ILDASM for disassembled output\n
  144. DEMO: May want to look in ILDASM for disassembled output\n
  145. DEMO: May want to look in ILDASM for disassembled output\n
  146. \n
  147. \n
  148. \n
  149. \n
  150. \n
  151. \n
  152. \n
  153. \n
  154. \n
  155. \n
  156. \n
  157. \n
  158. \n
  159. \n
  160. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  161. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  162. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  163. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  164. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  165. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  166. DEMO: May want to examine the following:\nExtensions\nNumberQueryWithExtensionMethod\n
  167. DEMO: May want to examine NumbersGrouping\n
  168. DEMO: May want to examine NumbersGrouping\n
  169. \n
  170. \n
  171. \n
  172. \n
  173. \n
  174. \n
  175. \n
  176. \n
  177. \n
  178. \n
  179. \n
  180. \n
  181. \n
  182. \n
  183. \n
  184. \n
  185. \n
  186. \n
  187. \n
  188. \n
  189. \n
  190. \n
  191. \n
  192. \n
  193. \n
  194. \n
  195. \n
  196. \n
  197. \n
  198. \n
  199. \n
  200. \n
  201. \n
  202. \n
  203. \n
  204. \n
  205. \n
  206. \n
  207. \n
  208. \n
  209. \n
  210. \n
  211. \n
  212. \n
  213. \n
  214. \n
  215. \n
  216. \n
  217. \n
  218. \n
  219. \n
  220. \n
  221. \n
  222. \n
  223. \n
  224. \n
  225. \n
  226. \n
  227. \n
  228. \n
  229. \n
  230. \n
  231. DEMO: rest of section\n