1 / 23

Chapter 7 – Arrays

Chapter 7 – Arrays. 7.1 Creating and Accessing Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays 7.5 A Case Study: Analyze a Loan. 7.2 Using LINQ with Arrays. Language INtegrated Queries The Distinct Operator The ToArray Method

tala
Download Presentation

Chapter 7 – Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays 7.5 A Case Study: Analyze a Loan

  2. 7.2 Using LINQ with Arrays • Language INtegrated Queries • The Distinct Operator • The ToArray Method • Use of Function Procedures in Queries • The Let Operator • The OrderBy Operator • The DataSource Property • Binary Search

  3. What is LINQ? • LINQ stands for Language INtegratedQuery • A query is a request for information. • Note: Option Infer must be set to ON in order to use LINQ Option Infer On

  4. LINQ Query Code of the form range variable DimqueryName = FromvarInarrayNamesource data Where [condition on var] Selectvar declares the variable queryName and assigns to it a sequence of the values from arrayName that satisfy the stated condition. query operators

  5. LINQ Query (continued) The values in the sequence can be converted to an array, displayed in a list box, or written to a text file.

  6. List only states with a length of five characters in the name. Query Example 'States.txt contains names of the 50 states Dim states() As String = IO.File.ReadAllLines("States.txt") Dim stateQuery = From state In states Where state.Length = 5 Select state For Each state As String In stateQuery lstStates.Items.Add(state) Next Output:Maine, Texas, Idaho

  7. Creating Query Results Dim states() As String = IO.File.ReadAllLines("States.txt") Dim stateQuery = From state In states Where state.Length = 5 Select state stateQuery(0) = “Maine” stateQuery(1) = “Texas” stateQuery(2) = “Idaho” The query checks each state from the array looking for matches where a state has 5 letters in it. Matching results (state) are placed into stateQuery.

  8. Using the Query Results Replace the For Each loop with lstStates.Items.Add(stateQuery.Count) lstStates.Items.Add(stateQuery.Min) lstStates.Items.Add(stateQuery(1)) Output:3, Idaho, Texas Query results can be accessed similar to arrays.

  9. LINQ Limitation • The main limitation of query results is its values may not be altered via assignment statements.

  10. Query Results Where Dim nums() As Integer = {5, 12, 8, 7, 11} Dim numQuery = From num In nums Where num > 7 Select num For Each num As Integer In numQuery lstBox.Items.Add(num) Next Output: 12, 8, 11

  11. Using Integer Query Results lstBox.Items.Add(numQuery.Min) lstBox.Items.Add(numQuery.First) Replace the For Each loop with: lstBox.Items.Add(numQuery.Sum) Output: 8, 12, 31

  12. Another Variation Where num > 7 Dim nums() As Integer = {5, 12, 8, 7, 11} Dim numQuery = From num Innums Where num > 7 Select num * num changed For Each num As Integer InnumQuery lstBox.Items.Add(num) Next Output: 144, 64, 121

  13. Distinct Operator Dim nums() As Integer = {5, 12, 5, 7, 12} Dim numQuery = From num In nums Select num Distinct For Each num As Integer In numQuery lstBox.Items.Add(num) Next Output: 5, 12, 7

  14. ToArray Method • A query variable is not an array variable. • The ToArray method converts it to an array variable. • This allows for array assignments. Dim nums() As Integer = {5, 12, 5, 7, 12} Dim numQuery = From num In nums Select num Dim numArray() As Integer = numQuery.ToArray

  15. Function Procedures in Queries Function procedures are commonly used in Where and Select clauses DimpresQuery = From pres In presidents WhereFirstName(pres) = txtFirstName.Text SelectIncludeTitle(pres) For Each pres InpresQuery lstPres.Items.Add(pres) Next

  16. Function Procedures in Queries (Continued) Function FirstName(ByVal nameAs String) As String 'Extract the first name from a full name. Dim parsedName() As String = name.Split(" "c) Return parsedName.First End Function

  17. Let Operator • A Let operator gives a name to an expression and makes queries easier to read. • Some situations arise that make the use of Let operators essential.

  18. Example of Let Operator Dim presQuery = From pres In presidents Select IncludeTitle(pres) can be replaced with Dim presQuery = From pres In presidents Let formalName = Includetitle(pres) Select formalName

  19. Order By Operator • Sorts string values into alphabetical order (either ascending or descending) • Sorts numbers into numeric order (either ascending or descending)

  20. Order By Operator (Integers) Dim nums() As Integer = {3, 6, 4, 1} Dim numQuery = From num In nums Order By num Ascending Select num For Each n As Integer In numQuery lstOutput.Items.Add(n) Next Output: 1, 3, 4, 6

  21. Order By Operator (Strings) Dim states() As String = IO.File.ReadAllLines("States.txt") Dim stateQuery = From state In states Order By state.Length Ascending, state Descending Select state For Each state As String In stateQuery lstStates.Items.Add(state) Next Output: Utah, Ohio, Iowa, Texas, Maine,...

  22. DataSource Property • The DataSource property fills a list box with the values returned by a query. lstBox.DataSource = queryName.ToList • The first entry will be highlighed. The highlighting can be eliminated with lstBox.SelectedItem = Nothing

  23. Listbox Shortcut Dim states() As String = IO.File.ReadAllLines("States.txt") Dim stateQuery = From state In states Order By state.Length Ascending, state Descending Select state lstStates.DataSource = stateQuery.ToList lstStates.SelectedItem = Nothing optional line Output: Utah, Ohio, Iowa, Texas, Maine, ...

More Related