Professional Documents
Culture Documents
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
Agenda
Language Integrated Query Structure of a LINQ Query Some LINQ Examples
Data Access
USE empl
REPLACE ALL salary WITH (salary * 1.1) FOR supervises > 0 LIST ALL fname, lname, salary FOR Supervises > 0
Object/Relational Mapping
Provides:
Mapping between relational data to/from objects Cleaner integration of business rules and validation
How do you enable clean code in both a strongly typed and dynamic language world?
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
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
LINQ Providers
C# uses LINQ providers to map your LINQ queries to the data source that youre 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
LINQ Overview
VB C# .NET Language-Integrated Query LINQ enabled data sources Others
Objects
Relational
XML
LINQ Providers
LINQ to Objects
Allows you to query in-memory of sets of objects
o o
Collections, arrays, and lists If a class implements IEnumerable (or generic version), you can use LINQ to query
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 Studios O/R designer to make it easy
o
For now, will mark up entity class manually Stores state for optimistic concurrency checks
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
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
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
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
Agenda
Language Integrated Query Structure of a LINQ Query Some LINQ Examples
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
Specify new instance of class in Select Compiler creates a new anonymous type containing just the properties you specify
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
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
Agenda
Language Integrated Query Structure of a LINQ Query Some LINQ Examples
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
LINQ requires ability to extend existing interfaces/classes without changing their definitions
Available because of new extension methods
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
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
See DemoLinqToSqlWithAnonymousTypes
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
See DemoNoLinqToXml
Examine CreateXmlContent
See CreateXmlContentVBOnly
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!