LINQ chapter 9 Dr Abraham Professor UTPA Problems

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 • Lists are similar to arrays but provide additional functionality: – Dynamic resizing – Provides many built in methods – Can use LINQ queries

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!)

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

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

TAKE STD DEV PROGRAM EXAMPLE • Reading the grades from a string. Dim grades() As String = Split(txt. Enter. Grade. 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. Get. Upper. Bound(0) scores(i) = Val(grades(i)) total += scores(i) lst. Box. Grades. Items. Add(scores(i)) Next

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

Some queries from the program • Dim sorted = From score In scores Order By score Ascending Where score > 4 Select score ‘ shows scores sorted • Dim gt. Ave = From score In scores Where score > average Select score ‘shows scores above average • To display For Each element In sorted lst. Box. Sorted. Items. Add(element)

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

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 list 1 as List(OF Integer)

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) trim. Excess

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

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

Array of objects • Dim employees as Employee() • The class employee may have – First. Name as string – Last. Name as string – SS as string – Salary as Decimal • See the program Demonstrated

LINQ to query array of Objects • Dim starts. With. R = • From item in items where item. Starts. With(“r”) order By item Select item. To. Upper() Selects colors that start with the letter r (then converts to upper case)
- Slides: 16