1 / 16

LINQ (chapter 9)

LINQ (chapter 9). Dr. Abraham Professor UTPA. 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. LIST class.

dalia
Download Presentation

LINQ (chapter 9)

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. LINQ (chapter 9) Dr. Abraham Professor UTPA

  2. 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.

  3. LIST class • Lists are similar to arrays but provide additional functionality: • Dynamic resizing • Provides many built in methods • Can use LINQ queries

  4. LINQ • Language-Integrated Query • Old way: SQL • Queries are written in string and passed to database which interpreted the string, processed the request and returned the results • LINQ allows a programming language to submit queries directly to a wide range of data sources (not just databases!)

  5. Querying an Array using LINQ • We use LINQ to Object querying to query a list (rather than LINQ to SQL or LINQ to XML) • Very similar to SQL • Dim filtered = From value in Values where value > 4 Select Value Dim sorted = from value in filtered order by value descending select value

  6. 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 such as counting, accessing, comparing and removing elements

  7. TAKE STD DEV PROGRAM EXAMPLE • 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

  8. Writing a query A LINQ query begins with a FROM clause, which specifies 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. Select clause determines what value appears in the results

  9. Some queries from the program • Dim sorted = From score In scores Order By score Ascending Where score > 4 Select score ‘ shows scores sorted • Dim gtAve = From score In scores Where score > average Select score ‘shows scores above average • To display For Each element In sorted lstBoxSorted.Items.Add(element) Next

  10. Using generic method to display LINQ • Generic methods allow to display variables of various types with a single method. • Please refer to textbook page 407 and 408 when using generic methods

  11. Introduction to Collections • .NET framework class library provides several classes called collections. • The clollection 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)

  12. 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

  13. Example • Dim items as new List(OF String) • Items.add(“red”) • Items.Insert(0,”yellow”) • See page 409 for additional help

  14. Querying a generic collection • Dim startswithR = from item in items where item.startswith(“r” order by item select item.toupper() For each item starswithR Console.write(item) Next Displays all items starts with r, but all in uppercase.

  15. Array of objects • Dim employees as Employee() • The class employee may have • FirstName as string • LastName as string • SS as string • Salary as Decimal • See the program Demonstrated

  16. LINQ to query array of Objects • Dim startsWithR = • From item in items where item.StartsWith(“r”) order By item Select item.ToUpper() Selects colors that start with the letter r (then converts to upper case)

More Related