Applied Linq - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Applied Linq

Description:

Uniform way to write queries over data. Linq is about query keywords ... General: Oakleaf Systems, Wes Dyer, Jomo Fisher. SQL: Mike Taulty. XML: Eric White. Summary ... – PowerPoint PPT presentation

Number of Views:265
Avg rating:3.0/5.0
Slides: 32
Provided by: alexth
Category:
Tags: applied | dyer | linq

less

Transcript and Presenter's Notes

Title: Applied Linq


1
(No Transcript)
2
Applied Linq
Putting Linq to work
3
Introducing
  • Class-A
  • Kennisprovider Microsoft development
  • Training
  • Coaching
  • http//www.class-a.nl
  • Alex Thissen
  • Trainer/coach
  • http//blog.alexthissen.nl

4
Agenda
  • About Linq and queries
  • Query providers and APIs
  • What it means to apply Linq
  • Language and code
  • Query execution
  • Recommendations
  • Architecture
  • Beyond Linq
  • Demos
  • Questions and discussion

5
Introducing Linq
  • Uniform way to write queries over data
  • Linq is about query keywords
  • Built into new languages C 3.0 and VB 9.0
  • Linq is about query operators
  • 40 standard query operators are defined
  • Methods that operate in queries or act on its
    results

Dim query From c In customers _
Where c.Orders.Count gt 100 _ Order By
c.CompanyName Descending _ Select c
var query from c in customers where
c.Orders.Count gt 100 orderby
c.CompanyName descending select c
6
About Linq queries
  • Expressed in terms of CLR types
  • Objects that might or might not exist in-memory
  • Linq queries are not SQL queries at all
  • Compositional and hierarchical by nature
  • Arbitrary nesting of queries
  • Additional operators can be applied to query
  • Declarative instead of imperative
  • Tell what you want, not how
  • How is up to underlying implementation

7
Writing queries
  • Learn the keywords and operators
  • Not all operators have corresponding keywords or
    language integration
  • Number of keywords may vary per language
  • Some parts must be written with operators
  • Usually exposes lambda expressions
  • Queries can be done without keywords
  • Explicit dot notation (what compiler creates)
  • Sometimes FLWOS is overkill

8
Query samples
var query from c in customers select
c.Orders.Sum(o gt o.Amount) var query2 (from c
in customers select c.Country).Distinct()
Customer someCustomer customers.FirstOrDefaul
t(c gt c.ID 1337)
Dim query _ From c In customers _ Select
c.Orders.Sum( _ Function (o As Order)
o.Amount) _ ) Dim query2 From c In customers
_ Select c.Country _ Distinct
  • VB Lambda expressions will appear in Beta 2

9
Demos
  • Getting familiar with Linq queries
  • Query operations selection, projection, grouping
  • Using keywords and operators

10
Sources of data
  • Your data must come from somewhere
  • In-memory CLR objects
  • Database
  • XML
  • Other repositories registry, Active Directory,
  • Various flavors of Linq disclose certain type of
    data
  • Query syntax does not change per source
  • Set of keywords and operators available might be
    different

11
Linq to XML
  • Power of Linq brought to data in XML format
  • Perform queries over XML data
  • New element-centric API to manipulate XML
  • Faster alternative to System.Xml DOM API
  • Functional construction of XML data with query
    expressions

12
Linq to SQL
  • Generates object model to represent data
  • Mapping of CLR types to database tables
  • Object/Relational Mapping (OR/M) technology
  • 11 relationships between objects and tables
  • Translates Linq queries to SQL statements
  • Builds on ADO.NET and .NET Transactions
  • Persistence services
  • Automatic change tracking and identity management
    of objects
  • Updates by SQL statements or stored procedures

13
Mapping strategy
  • Relationships map to collection properties
  • Single table inheritance is supported

Data context
Customer object
Order objects
14
Two kinds of Linq
15
Query providers
  • Out-of-the-box providers in .NET 3.5
  • Linq to Objects
  • Linq to SQL
  • Sources in query determine query providers
  • Affects keywords and operators you can use
  • Query providers can translate Linq queries to
    some implementation
  • Most providers come with Linq-enabled APIs
  • New object models to work with data
  • Linq to Objects has several Linq to XML, DataSets

16
Demo
  • Linq applied
  • Scenario WCF services using untyped Messages

WPF Application
WCF Service
SOAP messages
Linq to XML API
WCF proxy
Linq to XML
Linq to SQL
17
New language features C and VB
  • Linq keywords
  • Extension methods
  • Lambda expressions
  • Local variable inference
  • Object and collection initializers
  • Anonymous types
  • Automatic properties
  • Partial methods

new
new
18
Visual Basic 9.0
  • Deep XML support
  • Express XPath axes with XML properties
  • Allows XML literals to appear inside of code

root.ltCustomergt Direct child
elementscustomer._at_CompanyID Attribute
selectiondoc...ltdescriptiongt All descendants
Dim fragment As XElement _ ltcompanygt
ltcitygtHedellt/citygt lt/companygt
19
Visual Basic 9.0
  • Gives you expression holes inside XML literals
  • Additional keywords
  • More freedom on order of keywords

Dim query _ From c In doc...ltCustomersgt _
Select ltcustomer IDlt c._at_CustomerID gt gt
lt customer.ltNamegt gt phonelt
customer...ltPhonegt(0) gt lt/customergt
20
Demo
  • Visual Basic
  • WCF sample revisited
  • Deep XML support

21
Deferred execution
  • Most Linq query providers are implemented using
    lazy evaluation
  • Composed query expressions do not execute
    immediately
  • Queries only execute when necessary
  • Performing an iteration over resultset
  • Using extension methods ToArray, ToList,
    ToLookup, ToDictionary
  • Using specific query operators, such as
    aggregates and set operators

22
More on deferred execution
  • Before execution queries can still be changed or
    expanded
  • Adding additional sequence operators, such as
    Distinct, Skip, Take
  • Manipulating expression tree
  • Expression trees are immutable
  • Reference assignments take deep tree copies

23
Changes to way you write code
  • Declarative
  • Far less looping constructs visible
  • Easier to read and to maintain
  • Methods returning query or its results
  • Remember deferred execution
  • Force execution of query
  • Hand over query for further manipulation
  • Query reuse

24
Linq and architecture
System.Data.Linq
25
Architecture recommendations
  • Find out where and how your queries execute
  • Moment of execution
  • Local versus remote execution
  • Physical place/tier of query execution
  • Keep queries inside assemblies
  • Do not pass query expressions between layers
  • Anonymous types shouldnt be return values
  • Think about important types up front
  • Use projections wisely

26
Other recommendations
  • Learn
  • to write queries with and without keywords
  • new language features
  • Use whitepapers as reference
  • Translation of query keywords to operators
  • Evaluation details of operators
  • Do not overuse or abuse var keyword
  • Decreases readability and stops you from
    thinking if you know exact type, specify it
  • It sometimes takes more keystrokes

27
Runtimes and frameworks for Linq
providers

APIs
ADO.NET Entity Framework released out of
bounds with .NET FX 3.5
28
Beyond .NET FX 3.5
  • ADO.NET Entity Framework
  • Microsofts long-term vision on data
  • Brings query provider Linq to Entities
  • Parallel Linq (PLinq)
  • Passes parts of query to different cores in
    multicore/multiproc machine
  • Community query providers for Linq
  • Linq to Amazon, LDAP, SharePoint, NHibernate,
    MySql, Flickr, and more (to come)
  • Linq 2.0

29
Expanding on Linq
  • Linq-enable your existing APIs
  • Specifically for in-memory queries
  • Create extension methods that return an
    IEnumerableltTgt object
  • Write your own query provider
  • Implement IQueryableltTgt
  • Parse expression trees and translate nodes to
    different code or query language

30
References
  • Linq home page
  • Future versions of
  • C, Visual Basic, Visual Studio
  • Recommended reading
  • Linq to XML beta 1 docs
  • Future Linq 2.0, PLinq
  • Downloads
  • Linq May 2006 CTP (for Visual Studio 2005)
  • Visual Studio Orcas April 2007 Beta 1 and C, VB
    samples
  • Reflector 5.0

31
Blogs
  • Microsoft Teams
  • C
  • VB
  • ADO.NET
  • Individuals on Linq
  • General Oakleaf Systems, Wes Dyer, Jomo Fisher
  • SQL Mike Taulty
  • XML Eric White

32
Summary
  • Linq is about query keywords and operators
  • Linq will change way you write your code
  • Linq to XML might be more important than you
    think
  • Is VB a better query language than C?
  • Linq is extensible

33
Questions?
  • ?
Write a Comment
User Comments (0)
About PowerShow.com