This is without some of the graphics and
sound
WELCOME!
Finding the Missing LINQ:
the new query language in VB 2008
Thursday, March 12th
10:45-11:45, Veranda E
Diane Zak
Language INtegrated Query
Sounds impressive!
Why?
Software
Elements
Code
Visual Basic
C#
C++
Java
Data
text file
array
structure
class
database
“It was like you had to order
your dinner in one language
and drinks in another.”
Jason McConnell
VS Product Manager
I’ll have the
tuna salad
and a vaso de
té.
LinQ truly is the
missing link
LINQ
Code
Data
LINQ?
Access data from any data source
Arrays
Structures
Classes
Databases
XML, Collections, Objects, Lists, Dictionaries
LINQ?
Familiar
syntax
Less code
LINQ
Intellisense
support
Compile-time
checking
Why should I learn
LINQ?
Imperative language
 Assumes YOU know how
to solve the problem
Declarative language
 You state the problem
and the language decides
how to solve it
OK, I’m convinced!
Let’s get started…
Help YOU learn and teach LINQ
When can I start
teachiNG
LINQ?
Arrays
Structures
Classes
Databases
Time for your first
handout!
Basic
Syntax
results = From element In source
[Where condition]
[Order By element]
Select element
Order By
Clause
results = From element In source
[Where condition]
[Order By element]
Select element
Dim strMonths() As String = {“January”, “February”, … “December”}
Dim filter = From month In strMonths _
Order By month Select month
Be sure Option Infer is On
Displaying the
contents of the
For Each x In filter
collection
lstMonths.Items.Add(x)
Next x
results = From element In source
[Where condition]
[Order By element]
Select element
Dim strMonths() As String = {“January”, “February”, … “December”}
Dim filter = From month In strMonths _
Descending
month
Order By month Select
Where
Clause
filter
results = From element In source
[Where condition]
[Order By element]
Select element
Dim strMonths() As String = {“January”, “February”, … “December”}
Dim filter = From month In strMonths _
Where month = “June” Select month
results = From element In source
[Where condition]
[Order By element]
Select element
Dim strMonths() As String = {“January”, “February”, … “December”}
Dim filter = From month In strMonths _
Where month.Substring(0, 1) = “J” Select month
Where month Like “J*”
Where month.StartsWith(“J*”)
results = From element In source
[Where condition]
[Order By element]
Select element
Dim strMonths() As String = {“January”, “February”, … “December”}
Dim filter = From month In strMonths _
Where month.Length > 7 Order By month _
Select month
Structure monthInfo
Public strName As String
Public intDays As Integer
End Structure
Dim months(11) As monthInfo
months(0).strName = “January”
months(0).intDays = 31
months(11).strName = “December”
months(11).intDays = 31
Sort the month names in ascending
order, and then display them
Dim filter = From month In months _
Order By month.strName _
Select month.strName
For Each x In filter
lstMonths.Items.Add(x)
Next x
Sort the month names in ascending order,
and then display the names and days
Dim filter = From month In months Order By month.strName _
Select month
Select month.strName, month.intDays
For Each x In filter
lstMonths.Items.Add(x.strName.PadRight(15) & x.intDays)
Next x
Sort the months in descending order by
days, and then in ascending order by name
Dim filter = From month In months _
Order By month.intDays Descending, month.strName
Select the names of the months having 30 days
Dim filter = From month In months _
Where month.intDays = 30 Select month.strName
Select the names of the months having 30 days
and more than 5 characters
Dim filter = From month in months _
Where month.intDays = 30 _
AndAlso month.strName.Length > 5 _
Select month.strName
Intermissi
on
Candy!
MOVIE time
Movie
Answers
Properties of the MovieInfo class:
Quote (String)
Title (String)
Character (String)
Actor (String)
Filename (String)
Year (Integer)
Private movies(10) As MovieInfo
1. What is the title of the movie that
contains the selected quote?
Dim filter = From m In movies _
Where m.Quote = lstQuotes.SelectedItem _
Select m.Title
2. Which character said the selected quote?
Dim filter = From m In movies _
Where m.Quote = lstQuotes.SelectedItem _
Select m.Character
3. The selected quote is associated
with which actor and movie?
Dim filter = From m In movies _
Where m.Quote = lstQuotes.SelectedItem _
Select m.Title, m.Actor
4. How can you play the selected quote?
Dim filter = From m In movies _
Where m.Quote = lstQuotes.SelectedItem _
Select m.Filename
For Each x In filter
My.Computer.Audio.Play(x)
Next x
5. Which movies were made after 1996?
Dim filter = From m In movies _
Where m.Year > 1996 Select m.Title Distinct
A
B
Intermissi
on
Irish
Linq
Aggregation
Average
Count
operators
Max
Min
Sum
Basic
synta
x
Average
Count
Max
Min
Sum
Dim variable [As dataType] = Aggregate element In source
[Where condition]
Select element Into aggregationOperator
Dim variable [As dataType] = Aggregate element In source
[Where condition]
Select element Into aggregationOperator
Dim intClovers() As Integer= {0, 0, 1, 2, 5, 6, 8, 4, 3, 2, 1, 0}
Dim intSum As Integer = Aggregate clover In intClovers _
Select clover Into Sum()
Dim variable [As dataType] = Aggregate element In source
[Where condition]
Select element Into aggregationOperator
Dim intClovers() As Integer= {0, 0, 1, 2, 5, 6, 8, 4, 3, 2, 1, 0}
Dim dblAvg As Double = Aggregate clover In intClovers _
Select clover Into Average()
Dim variable [As dataType] = Aggregate element In source
[Where condition]
Select element Into aggregationOperator
Dim intClovers() As Integer= {0, 0, 1, 2, 5, 6, 8, 4, 3, 2, 1, 0}
Dim intCount As Integer = Aggregate clover In intClovers _
Where clover = 0 _
Select clover Into Count()
Structure popInfo
Public intGroup As Integer
Public intMales As Integer
Public intFemales As Integer
End Structure
Dim pop2006(4) As popInfo
1. How many males in the population?
Dim intTotalMales As Integer = _
Aggregate m In pop2006 _
Select m.intMales Into Sum()
2. What is the total population?
Dim intTotal As Integer = _
Aggregate m In pop2006 _
Select m.intMales + m.intFemales _
Into Sum()
3. How many groups of females contain over
450,000 people?
Dim intCount As Integer = _
Aggregate c In pop2006 _
Where c.intFemales > 450000 _
Select c.intFemales Into Count()
4. Which group has the most males?
Dim intMostMales As Integer = _
Aggregate m In pop2006 _
Select m.intMales Into Max()
Dim strMostMales = From pop In pop2006 _
Where pop.intMales = intMostMales _
Select pop.strGroup
5. What is the percentage of females?
Dim intTotalFem As Integer = Aggregate f In pop2006 _
Select f.intFemales Into Sum()
Dim intTotal As Integer = Aggregate t In pop2006 _
Select t.intFemales + t.intMales Into Sum()
Dim dblAvg As Double = intTotalFem / intTotal
Intermissi
on
the king of
Elvis
Database
Movies table
ID
Title
Released
Studio Text
Auto
Text
Date
Sample record:
15
Viva Las Vegas
5/6/1964
MGM
You are looking
a bit frazzled!
1. How many movies did Elvis make?
Dim intCount As Integer = _
Aggregate m In ElvisDataSet.Movies _
Select m Into Count()
2. How many of his movies were released
in 1965?
Dim intCount As Integer = _
Aggregate m In ElvisDataSet.Movies _
Where m.Released.ToShortDateString.EndsWith("1965") _
Select m Into Count()
3. Which movie was produced by Allied
Artists?
Dim filter = From m In ElvisDataSet.Movies _
Where m.Studio = “Allied Artists” _
Select m.Title
4. How many of his movies were
produced by MGM?
Dim intCount As Integer = _
Aggregate m In ElvisDataSet.Movies _
Where m.Studio = “MGM”
Select m Into Count()
5. When was Love Me Tender released?
Dim filter = From m In ElvisDataSet.Movies _
Where m.Title = “Love Me Tender” _
Select m.Released
6. What studio produced Clambake?
Dim filter = From m In ElvisDataSet.Movies _
Where m.Title = “Clambake” _
Select m.Studio
7. What movie was released on
11/10/1957?
Dim filter = From m In ElvisDataSet.Movies _
Where m.Released = #11/10/1957# _
Select m.Title
8. What was his last movie?
Dim dtmLast As Date = _
Aggregate m In ElvisDataSet.Movies _
Select m.Released Into Max()
Dim filter = From m In ElvisDataSt.Movies _
Where m.Released = dtmLast Select m.Title
9. How many of his movies have oneword titles?
Dim intCount As Integer = _
Aggregate m In ElvisDataSet.Movies _
Where Not m.Title.Contains(“ ”) _
Select m.Title Into Count()
It’s Time
for
Elvis bingo
1. How many movies did Elvis make?
2. How many of his movies were released
in 1965?
3. Which movie was produced by Allied
Artists?
4. How many of his movies were
produced by MGM?
5. When was Love Me Tender released?
6. What studio produced Clambake?
7. What movie was released on
11/10/1957?
8. What was his last movie?
9. How many of his movies have oneword titles?
He’ll always be the King of Rock ‘n’ Roll
I hope you enjoyed this presentation
on LINQ.
Thank you
for being such a great audience!
Diane Zak
Dim intSales() As Integer = {100, 200, 300}
Dim dblRate() As Double = {0.05, 0.1, 0.4}
Dim filter = From sales In intSales _
From rate In dblRate _
Let commission = sales * rate _
Select sales, rate, commission
Dim strData() As String = _
{"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}
Dim filter = From x In strData _
Group x By FirstLetter = x.Substring(0, 1) _
Into Group Order By FirstLetter Select FirstLetter, words = Group
For Each letterGroup In filter
lblInfo.Text = lblInfo.Text & letterGroup.FirstLetter & ControlChars.NewLine
For Each w In letterGroup.words
lblInfo.Text = lblInfo.Text & " " & w & ControlChars.NewLine
Next w
Next letterGroup
Descargar

Document