csci 3327 visual basic chapter 8: introduction to linq and collections utpa – fall 2011

20
CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 8: Chapter 8: Introduction to LINQ Introduction to LINQ and Collections and Collections UTPA – Fall 2011

Upload: octavia-bell

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ Chapter 8: Introduction to LINQ

and Collectionsand Collections

UTPA – Fall 2011

Page 2: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Objectives

• In this chapter, you will:– Become familiar with the basic concept of LINQ– Learn how to use LINQ to query an array– Learn how to sort an array using LINQ– Learn how to manipulate collections (e.g., List) by

LINQ

2

Page 3: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

LINQ

• Language Integrated Query (LINQ)• Old way:

– Structured Query Language (SQL)– Queries are written in string and passed to database

which interprets the string, processes the request, and returns the results

• LINQ allows a programming language to submit queries directly to a wide range of data sources (not just databases!)

Page 4: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

LINQ (cont'd)

• LINQ is a logical extension of querying and manipulating data in databases

• Data sources– Arrays– Collections

• Items collection of a ListBox

– Files

• Query expression is similar to SQL

4

Page 5: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Writing an LINQ Query

• A LINQ query begins with a From clause– After From clause, specify a range variable and the

data source to query– The range variable represents each item in the data

source (much like For…Each Next)

• The Where clause evaluates to True or False – If True, the value is selected

• The Select clause determines what value appears in the results

Page 6: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Querying an Array of Primitive-Type Elements Using LINQ

• Dim filtered = From value In values

Where value > 4

Select value

• Dim sorted = From value In filtered

Order By value

Select value default: Ascending

Page 7: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Querying an Array of Primitive-Type Elements Using LINQ (cont'd)

• Dim sorted = From value In filtered

Order By value Descending

Select value

• Dim sorted = From value In values

Where (value > 4)

Order By value Descending

Select value

Page 8: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Example 11.2: LINQWithArrayOfIntegers.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

• Keywords in LINQ– From … In– Where– Select– Order By … Descending

• Chained vs. one query

8

Page 9: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

LINQ Providers

• LINQ provider is a layer between the LINQ engine and the data source

• It consists of a set of classes that implement operation needed by LINQ – Generic methods allow to display variables of

various types with a single method– E.g., counting, accessing, comparing, and

removing elements

Page 10: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Example 11.3-11.4: LINQWithEmployeeArray.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

• nameSorted.Count()• nameSorted.First()• Keyword: Distinct• Anonymous types

– The new class that does not have a name and cannot be used to create new objects

Page 11: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Other IEnumerable Extension Methods

• Any

• Average

• Cast

• Contains

• Count

• Distinct

• ElementAt

• First

• Last

• Max

• Min

• Reverse

• Sum

Page 12: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Example 11.7: DeferredExecution.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

• String – Color.StartsWith("r")– Color.ToUpper()

• The changes to the data source will be included when we re-execute the query

12

Page 13: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Introduction to Collections

• .NET framework class library provides several classes called collections

• The collection class List (OF T) comes from namespace System.Collections.Generic– The T is the placeholder change as appropriate as:– Dim list1 As List (OF Integer)

Page 14: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Problems With Arrays

• Must specify size of array

• Only some languages allow redimesioning it at runtime

• .NET framework’s collection class overcomes this limitation

• We are going to use the List class from this collection

Page 15: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

List Class

• Lists are similar to arrays but provide additional functionality:– Dynamic resizing– Provides many built-in methods– Can use LINQ queries

Page 16: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Property or Method of List

• Add (to the end)• Capacity (how many)• Clear (remove all)• Contains(returns true or false)• Count (how many)• Indexof (first occurrence of specified value in list)• Insert (add at specified index)• trimExcess

Page 17: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Example

• Dim items as new List(OF String)

• Items.Add(“red”)

• Items.Insert(0,”yellow”)

Page 18: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

Querying a Generic Collection

• Dim startswithR = From item In items Where item.StartsWith(“r”)

Order By item Select item.ToUpper()

For Each item In startswithR Console.Write(item)

Next• Displays all items starts with r, but all in uppercase

Page 19: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

STD DEV Program in Your Assignment

• Reading the grades from a string Dim grades() As String = Split(txtEnterGrade.Text, " ")• Now let us create our own array instead of using grades()

which is an array of string Dim scores(80) As Integer For i = 0 To grades.GetUpperBound(0) scores(i) = Val(grades(i)) total += scores(i) lstBoxGrades.Items.Add(scores(i)) Next

Page 20: CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011

20