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

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

LINQ and Collections An introduction to LINQ and Collections.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Programming with Collections Collections in Java Using Arrays Week 9.
Chapter 7: Working with Arrays
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 3 Single-Table Queries
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1A) UTPA – Fall 2011.
Chapter 9: Advanced Array Concepts
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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:
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Concepts of Database Management Seventh Edition
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Binding to ListBox and ComboBox Controls List and combo boxes are frequently.
Lecture 10 Collections Richard Gesick.
Introduction to LINQ and Generic Collections
Chapter 7: Working with Arrays
Visual Basic 2010 How to Program
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
CSCI 3327 Visual Basic Chapter 7: Arrays
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Introduction to LINQ Chapter 11.
CSCI 3327 Visual Basic Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections UTPA – Fall 2012 This set of slides is revised from lecture slides.
Object Oriented Programming in java
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Topics Sequences Introduction to Lists List Slicing
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Fundaments of Game Design
Topics Sequences Introduction to Lists List Slicing
LINQ - 2 Ravi Kumar C++/C# Team.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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

Example 11.2: LINQWithArrayOfIntegers.vb URL: – p_2010/codeexamples.htmlhttp://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

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

Example : LINQWithEmployeeArray.vb URL: – p_2010/codeexamples.htmlhttp://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

Other IEnumerable Extension Methods Any Average Cast Contains Count Distinct ElementAt First Last Max Min Reverse Sum

Example 11.7: DeferredExecution.vb URL: – p_2010/codeexamples.htmlhttp://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

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)

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

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

Example Dim items as new List(OF String) Items.Add(“red”) Items.Insert(0,”yellow”)

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

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

20