Introduction to LINQ Chapter 11.

Slides:



Advertisements
Similar presentations
LINQ and Collections An introduction to LINQ and Collections.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
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.
 2006 Pearson Education, Inc. All rights reserved Generics.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
ASP.NET Programming with C# and SQL Server First Edition
Using the Select Case Statement and the MsgBox Function (Unit 8)
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Chapter 2: Using Data.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Introduction to Java Java Translation Program Structure
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
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.
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.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Visual Basic 2010 How to Program
Arrays Chapter 7.
Introduction to LINQ and Generic Collections
Object-Oriented Programming: Classes and Objects
 2012 Pearson Education, Inc. All rights reserved.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Microsoft Visual Basic 2005: Reloaded Second Edition
ATS Application Programming: Java Programming
Chapter 20 Generic Classes and Methods
Problem Solving and Control Statements: Part 2
Visual Basic 2010 How to Program
Visual Basic 2010 How to Program
LINQ to DATABASE-2.
JDBC.
Object-Oriented Programming: Classes and Objects
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
LINQ to DATABASE-2.
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
Chapter 6 Control Statements: Part 2
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
CIS16 Application Development and Programming using Visual Basic.net
Methods.
Arrays Part 2.
Object Oriented Programming in java
Java Programming Language
Chapter 3 Introduction to Classes, Objects Methods and Strings
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.
Review of Java Fundamentals
Presentation transcript:

Introduction to LINQ Chapter 11

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.  Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data contained in the database. Today’s most popular database systems are relational databases. A language called Structured Query Language (SQL) is an international standard used with relational databases to perform queries (that is, to request information that satisfies given criteria) and to manipulate data. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved. Introduction For years, programs that accessed a relational database passed SQL queries as Strings to the database management system then processed the results. Microsoft developed LINQ (Language Integrated Query) to enable you to write query expressions similar to SQL queries that retrieve information from a wide variety of data sources—not just relational databases—using a common syntax that is built into Visual Basic. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved. Introduction We use LINQ to Objects to query the contents of arrays, selecting elements that satisfy a set of conditions—this is known as filtering. We also use LINQ to Objects to perform common array manipulations such as sorting an array. Figure 11.1 shows the types of LINQ queries. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ Primitive types are the basic type of data Byte, short, int, long, float, double, boolean, char Primitive variables store primitive values. LINQ allows you to look at collections of data, extract information and manipulate data. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ Example: Dim values() As Integer = {2,9,5,0,3,7,1,4,8,6} Dim filtered = From value In values where (value > 4) Select value © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ Our first LINQ query begins with a From clause which specifies a range variable (value) and the data source to query (the array values). The range variable represents each item in the data source, much like the control variable in a For Each…Next statement. If the condition in the Where clause evaluates to True, the element is selected—that is, it’s included in the collection of Integers that represents the query results. Here, the Integers in the array are included only if they’re greater than 4. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ For each item in the data source, the Select clause determines what value appears in the results. In this case, it’s the Integer that the range variable currently represents. The Select clause is usually placed at the end of the query for clarity, though it may be placed after the From clause and before other clauses, or omitted. If omitted, the range variable is implicitly selected. The Select clause can transform the selected items—for example, Select value * 2 in this example would have multiplied each selected value in the result by 2. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ You can use a For Each…Next statement to iterate over the results of any LINQ query. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ Sorting LINQ Query Results The LINQ query in the above example selects the elements of the array values and returns an IEnumerable object containing a sorted copy of the elements. Dim values() As Integer = {2,9,5,0,3,7,1,4,8,6} Dim sorted = From value In values Order By value Select value © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ The Order By clause sorts the query results in ascending order. You can use the Descending modifier in the Order By clause to sort query results in descending order. An Ascending modifier also exists but is rarely used, because it’s the default. You can use the Order By clause only for values that can be compared to one another. The Order By clause supports values of any type that implements the interface IComparable, such as the primitive numeric types and String. Such types provide a CompareTo method.

Querying an Array of Reference-Type Elements Using LINQ LINQ is not limited to querying arrays of primitive types. It can be used with most data types.

Example

Example

Example

Example

Example

Example

Example

Querying an Array of Reference-Type Elements Using LINQ When you type the name of an IEnumerable object (such as an array or the result of a LINQ query) then type the dot (.) separator, the list of the methods and properties that can be used with that object are shown. Some of the methods are so-called extension methods. For example, if you have an array of Doubles called numbers and you want to calculate the average of its values, you can simply call the Average extension method, as in numbers.Average(). Some of IEnumerable’s 45 extension methods are shown in Fig. 11.5.

Creating Objects of Anonymous Types Example: Dim names= From Employee In employees Select Employee.firstName, Employee.lastName

Creating Objects of Anonymous Types The LINQ query in this example selects only the FirstName and LastName from each Employee object. You can select portions of matching objects by specifying the properties to select in a comma-separated list. Only the selected instance can be accessed when iterating over the query results. When you select a portion of an object’s instance, the compiler creates a new class containing those instance—FirstName and LastName in this example—and the methods that are inherited by all classes from class Object The new class does not have a name and cannot be used by you to create new objects—such classes are called anonymous types.

Deferred Execution and Transforming Query Results LINQ uses a technique called deferred execution—a query executes only when you iterate over the results, not when the query is defined. This allows you to create a query once and execute it many times. If you make any changes to the data in a LINQ query’s data source, the next time you iterate over the query’s results, the query will process the current data in the data source. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Deferred Execution and Transforming Query Results Figure 11.7 filters an array of Strings by searching for those that begin with "r". Initially the array (lines 8–9) contains two such Strings. Later in the program we modify the array then reexecute the LINQ query to demonstrate deferred execution. This example also demonstrates how to transform the items that match the Where clause—in this case, each matching String is converted to uppercase in the query result. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.