Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.

Slides:



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

Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming with Collections Collections in Java Using Arrays Week 9.
 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.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
ASP.NET Programming with C# and SQL Server First Edition
Using the Select Case Statement and the MsgBox Function (Unit 8)
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Chapter 2: Using Data.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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
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.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
Visual Basic 2010 How to Program
Introduction to LINQ and Generic Collections
 2012 Pearson Education, Inc. All rights reserved.
Problem Solving and Control Statements: Part 2
Visual Basic 2010 How to Program
LINQ to DATABASE-2.
JDBC.
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.
MSIS 655 Advanced Business Applications Programming
LINQ to DATABASE-2.
Introduction to LINQ Chapter 11.
Object Oriented Programming in java
Chapter 6 Control Statements: Part 2
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.
Presentation transcript:

Introduction to LINQ Chapter 11

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. © by Pearson Education, Inc. All Rights Reserved.

Introduction For years, programs that accessed a relational database passed SQL queries as String s 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. © 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. © 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. © by Pearson Education, Inc. All Rights Reserved.

Querying an Array of Primitive-Type Elements Using LINQ Example: © by Pearson Education, Inc. All Rights Reserved. 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

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 Integer s that represents the query results. Here, the Integer s in the array are included only if they’re greater than 4. © 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. © 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. © 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. © by Pearson Education, Inc. All Rights Reserved. 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

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

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 Double s 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

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. © by Pearson Education, Inc. All Rights Reserved.

Deferred Execution and Transforming Query Results Figure 11.7 filters an array of String s by searching for those that begin with "r". Initially the array (lines 8–9) contains two such String s. 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. © by Pearson Education, Inc. All Rights Reserved.