840 likes | 913 Views
This is without some of the graphics and sound. WELCOME! Finding the Missing LINQ: the new query language in VB 2008. Thursday, March 12 th 10:45-11:45, Veranda E Diane Zak. LINQ. L anguage IN tegrated Q uery. Sounds impressive!. WHAT?. Why?. Software Elements. Code. Data.
E N D
WELCOME!Finding the Missing LINQ:the new query language in VB 2008 Thursday, March 12th 10:45-11:45, Veranda E Diane Zak
LINQ Language INtegrated Query
Sounds impressive! WHAT? Why?
Software Elements Code Data Visual Basic C# C++ Java 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
Why should I learn LINQ? Access data from any data source Arrays Structures Classes Databases XML, Collections, Objects, Lists, Dictionaries
Why should I learn LINQ? Imperative language Declarative language You state the problem and the language decides how to solve it • Assumes YOU know how to solve the problem
OK, I’m convinced! Let’s get started…
My goal? Help YOU learn and teach LINQ
When can I start teachiNG LINQ? Arrays Structures Classes Databases
Basic Syntax results= From elementInsource [Wherecondition] [Order By element] Selectelement
results= From elementInsource [Wherecondition] [Order By element]Selectelement Dim filter =From month InstrMonths _ Order By month Select month Dim strMonths() As String = {“January”, “February”, … “December”} Be sure Option Infer is On
Displaying the contents of the collection For Each x In filter lstMonths.Items.Add(x) Next x
results= From elementInsource [Wherecondition] [Order By element]Selectelement Dim filter = From month In strMonths _ Order By month Dim strMonths() As String = {“January”, “February”, … “December”} Select month Descending
Where Clause filter
results= From elementInsource [Wherecondition] [Order By element]Selectelement Dim filter = From month In strMonths _ Select month Dim strMonths() As String = {“January”, “February”, … “December”} Where month = “June”
results= From elementInsource [Wherecondition] [Order By element]Selectelement Dim filter = From month In strMonths _ Select month Dim strMonths() As String = {“January”, “February”, … “December”} Where month.Substring(0, 1) = “J” Where month Like “J*” Where month.StartsWith(“J*”)
results= From elementInsource [Wherecondition] [Order By element]Selectelement Dim filter = From month In strMonths _ _ Select month Dim strMonths() As String = {“January”, “February”, … “December”} Where month.Length > 7 Order By 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
Intermission Candy! Candy!
Properties of the MovieInfo class: Quote (String) Title (String) Character (String) Actor (String) Filename (String) Year (Integer) Private movies(10) As MovieInfo
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
Which character said the selected quote? Dim filter = From m In movies _ Where m.Quote = lstQuotes.SelectedItem _ Select m.Character
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
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 Next x My.Computer.Audio.Play(x)
Which movies were made after 1996? Dim filter = From m In movies _ Where m.Year > 1996 Select m.Title Distinct A B
Intermission Irish mints!
Linq Aggregation operators Luck of the Irish Average Count Max Min Sum
Luck of the Irish Basic syntax Average Count Max Min Sum Dimvariable[AsdataType]= Aggregate elementInsource [Wherecondition] SelectelementIntoaggregationOperator
Dimvariable[AsdataType]= Aggregate elementInsource [Wherecondition] SelectelementIntoaggregationOperator 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()
Dimvariable[AsdataType]= Aggregate elementInsource [Wherecondition] SelectelementIntoaggregationOperator 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()