LINQ: the new way to query - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

LINQ: the new way to query

Description:

Anonymous Types feature uses another VB9 feature called Object Initializers. Object Initializers let you initialize a complex data object within an expression. ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 26
Provided by: novaco
Category:
Tags: linq | featurelet | new | query | way

less

Transcript and Presenter's Notes

Title: LINQ: the new way to query


1
LINQ the new way to query
  • LINQ Project (Language-Integrated Query) A
    general purpose query facility of the .NET
    Framework.
  • Presented byDwight Barbour, dbs_at_dbsolutions.net
    http//www.eeicom.com/traininghttp//www.FederalA
    SP.org/202.250.3489
  • NoVa Code Camp 2007

2
Dwight Barbour
  • Educator
  • GW, GT, UDC, USDA
  • Independent Technology Schools
  • Customized Training for Government, DoD,
    Non-profit and private industry
  • Developer
  • Data-driven Web Development
  • Accessibility (Section 508, 1194.22 and W3C WAI)

3
Audience Check
  • Developing for over 5 years?
  • Works with data?
  • Database
  • XML source
  • .NET Developer?
  • ASP.NET?
  • What language?

4
Code Samples
  • ASP.NET with Visual Basic
  • Samples are focused
  • Slides and code are availableDrop me an email
    ltdbs_at_dbsolutions.netgt for the slides and code.
    If you email within a week, I'll send you a
    discount coupon for a class at EEI Training.
    http//www.eeicom.com/

5
Goals
  • What is LINQ?
  • How to get started
  • .NET 3.0 Language EnhancementsDemos are in VB
    9.0 .NET Visual Basic Versions
    VB 9.0 VB.NET 3.0 VB 8.0 VB.NET 2.0
    VB 2005 VB 7.0 VB.NET 1.x VB 2003
  • Introduction to the LINQ syntax
  • Advanced Demos

6
Pre-LINQ Scenario
  • PROGRAMMERS
  • are becoming more productive with objects
  • With objects we work faster. inheritance
    IntelliSensing
  • TECHNOLOGIES
  • SQL (a SQL string) to work with XML
  • Xquery/XPath to work with XML
  • Methods/Functions to work with in- memory
    objects

7
(1) What is LINQ?
  • General purpose query facility of .NET Framework
  • Applicable to multiple data sources
  • Relational databases / DataSets
  • XML
  • In memory objects (array, hash, collection)
  • Talk ClarificationWe are focusing on Query, but
    Create and Update capabilities exist too.

8
(2) How to get started
  • Downloads Community Technology Preview (CTP) May
    2006 (or upgrade to 3.0)http//msdn.microsoft.com
    /XML/linqproject
  • Installs (WARNING)
  • Language Enhancements (VB C)
  • LINQ Functionality
  • Samples
  • Documents
  • LINQ Project Type
  • Create a new LINQ Project Type

9
LINQ Project Type
10
(3) VB 9.0 Enhancements
  • Type Inference (Implicit Typing)
  • Anonymous Types
  • Object Initializers

11
What is Type Inference (Implicit Typing)
  • Type a data typeCommon types include primitive
    types (integers, floats, characters), classes,
    function types, etc.
  • Inference guessingAct of deriving logical
    conclusion
  • Implicit not stated, but understoodUnderstood
    though not directly expressed

12
Type Inference(Implicit Typing)
  • ' VB8
  • Dim state As String DC
  • Dim states() As String DC, VA, MD
  • ' VB9
  • Dim state DC
  • Dim states DC, VA, MDNOTE Type
    inference is determined at compile time, it is
    NOT late binding(runtime).

13
What areAnonymous Types
  • Anonymous types lets you create variables of new
    types without first defining the type. Because
    the new type is never explicitly specified, it
    remains anonymous.
  • Anonymous TypeHaving an unknown type.

14
Object Initializers
  • Anonymous Types feature uses another VB9 feature
    called Object Initializers.
  • Object Initializers let you initialize a complex
    data object within an expression.
  • For the demo, think of this Customer Object
    (instance called cust1) cust1.Name
    cust1.City cust1.State

15
Object Initializers
  • Object Initializer all fieldsDim cust1 As
    Customer New Customer NameJoe,
    CityNYC, StateNY
  • Object Initializer some fieldsDim cust1 As
    Customer New Customer NameJoe,
    StateNY

16
Object Initializers withType Inference
  • Left-Side Type Inference (infer Customer) Dim
    cust1 New Customer NameJoe,
    StateNY
  • Right-Side Type Inference (infer Customer) Dim
    cust1 As Customer NameJoe,
    StateNY
  • Both-Side Type Inference (infers Anonymous
    Type!) Dim cust1 New NameJoe,
    StateNY

17
(4) LINQ Syntax
  • Basic Syntax
  • Sorting
  • Aggregate Operators (count, min, max, sum,
    average)
  • Partitioning OperatorsSelect a portion of the
    collection by inclusion or exclusion.
  • Element OperatorsChoose one specific element
    from the collection.

18
Basic Query Syntax
  • Dim theNums 1, 10, 21, 2
  • Dim underTen From n In theNums _ Where n lt 10
    _ Select n
  • For Each Dim m In underTen label1.Text m
    Next
  • Output 1 2
  • Note Implicit Types theNums (integer array),
    underTen (IEnumerable of Integer), n (integer)

19
Sorting a Query
  • Dim theNums 1, 9, 13, 7, 2
  • Dim underTen From n In theNums _ Where n lt 10
    _ Select n _ Order By n
  • Dim underTen From n In theNums _ Where n lt 10
    _ Select n _ Order By n Ascending
  • Dim underTen From n In theNums _ Where n lt 10
    _ Select n _ Order By n Descending

20
Aggregate Operators (Count, Min, Max, Sum,
Average)
  • Dim underTenCount Count( From n In theNums _
    Where n lt 10 _ Select n )
  • Dim underTenCount ( From n In theNums _ Where
    n lt 10 _ Select n ).Count()Which form of the
    aggregate operator would you prefer?

21
Partitioning Operators Select a portion of a
collection by inclusion or exclusion.
  • Dim underTenCount ( From n In theNums _ Where
    n lt 10 _ Select n _ Order By n ).Take(5)
  • Dim underTenCount ( From n In theNums _ Where
    n lt 10 _ Select n _ Order By n ).Skip(5)

22
Element Operators Select a single element to be
returned.
  • Dim firstFoundUnderTen ( From n In theNums _
    Where n lt 10 _ Select n ).First()
  • Dim zeroBased ( From n In theNums _ Where n lt
    10 _ Select n ).ElementAt(5)Note Position 5
    is the index value. Positions are zero based.
    Position 5 is actually the 6th item.

23
(5) Advanced Demos
  • Structured Data
  • Multiple Source
  • Group By

24
LINQ Review
  • What is LINQ?
  • It is used to....
  • The installation installs...
  • Benefits
  • Transferable Knowledge
  • IntelliSense

25
Dwight Barbourdbs_at_dbsolutions.net 202.250.3489
  • Training needs? Custom dbs_at_dbsolutions.net,
    202.250.3489Open Enrolment
    http//www.eeicom.com/training/
  • Free Traininghttp//www.FederalASP.org/http//w
    ww.eeicom.com/training/seminar_register.cfm
Write a Comment
User Comments (0)
About PowerShow.com