cs313d: advanced programming language · pdf filecs313d: advanced programming language ... sql...
TRANSCRIPT
![Page 1: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/1.jpg)
CS313D: ADVANCED
PROGRAMMING LANGUAGE
Lecture 10: introduction to LINQ Computer Science
department
![Page 2: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/2.jpg)
Lecture Contents
dr. Amal Khalifa, Spr 17
2
What is LINQ?
What is a query?
Query on arrays
Query on a Generic List
![Page 3: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/3.jpg)
Query
dr. Amal Khalifa, Spr 17
3
A query: to request information that
satisfies given criteria.
SQL is the international standard used to
perform and to manipulate data in a database.
LINQ (Language-Integrated Query) allow to
write query expressions that retrieve
information from a variety of data sources, not
just databases.
LINQ to Objects can be used to filter arrays and
Lists, selecting elements that satisfy a set of
conditions
![Page 4: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/4.jpg)
Using LINQ on Arrays
dr. Amal Khalifa, Spr 17
4
Repetition statements that filter arrays focus on the
steps required to get the results imperative
programming.
LINQ queries, specify what to do not how to do it
declarative programming
![Page 5: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/5.jpg)
Example
from
specifies a
range
variable and
the data
source to
query
the where
specifies the
condition
select
determines
the results
dr. Amal Khalifa, Spr 17
5
![Page 6: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/6.jpg)
Example
The orderby
clause sorts
the query
results in
ascending
order.
The
descending
modifier sorts
the results in
descending
order.
dr. Amal Khalifa, Spr 17
6
![Page 7: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/7.jpg)
Example
A LINQ query
returns an
IEnumerable<T>
object
dr. Amal Khalifa, Spr 17
7
![Page 8: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/8.jpg)
dr. Amal Khalifa, Spr 17 8
![Page 9: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/9.jpg)
Using LINQ on an Array of Objects
dr. Amal Khalifa, Spr 17
9
LINQ is not limited to querying arrays of simple
types such as integers.
The following example uses LINQ to query an
array of Employee objects.
![Page 10: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/10.jpg)
The Employee Class
dr. Amal Khalifa, Spr 17
10
![Page 11: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/11.jpg)
dr. Amal Khalifa, Spr 17 11
![Page 12: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/12.jpg)
Test App
dr. Amal Khalifa, Spr 17
12
![Page 13: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/13.jpg)
Test App
conditional
AND (&&)
operator can
be used to
combine
conditions.
An orderby
clause can
sort the results
according to
multiple
properties
dr. Amal Khalifa, Spr 17
13
![Page 14: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/14.jpg)
Test App
Any checks if there is at least one element.
First returns the first element in the result.
Distinct removes duplicate elements
Projection performs a transformation on the data
dr. Amal Khalifa, Spr 17
14
![Page 15: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/15.jpg)
Test App
dr. Amal Khalifa, Spr 17
When the
compiler
creates an
anonymous
type, it
automatically
generates a
ToString
method that
returns a
string
representation
of the object.
15
![Page 16: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/16.jpg)
dr. Amal Khalifa, Spr 17 16
![Page 17: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/17.jpg)
Querying a Generic Collection
dr. Amal Khalifa, Spr 17
17
You can use LINQ to Objects to query List just as arrays.
Although commonly used, arrays have limited capabilities.
A List is similar to an array but provides additional functionality, such as dynamic resizing.
![Page 18: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/18.jpg)
Example
let used to
create a new
range
variable to
store a
temporary
result for use
later in the
LINQ query.
dr. Amal Khalifa, Spr 17
18
![Page 19: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/19.jpg)
dr. Amal Khalifa, Spr 17 19
![Page 20: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/20.jpg)
Chapter 9: 9.1, 9.2, 9.3, 9.5
That’s all
dr. Amal Khalifa, Spr 17
20
![Page 21: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/21.jpg)
Case Study 21
dr. Amal Khalifa, Spr 17
![Page 22: CS313D: ADVANCED PROGRAMMING LANGUAGE · PDF fileCS313D: ADVANCED PROGRAMMING LANGUAGE ... SQL is the international standard used to ... Chapter 9: 9.1, 9.2, 9.3, 9.5](https://reader031.vdocuments.net/reader031/viewer/2022021816/5a6fb5937f8b9abb538b5165/html5/thumbnails/22.jpg)
Q:
dr. Amal Khalifa, Spr 17
22