Вы находитесь на странице: 1из 19

ISM 6225

Distributed
Information Systems
LECTURE 4 – LINQ
Agenda
• Introduction

• Examples

• Exercises

2
Introduction
• LINQ is Language INtegrated Query
◦ Attempts to provide a uniform data query language for all common data
sources
◦ Databases, XML etc
◦ Using a syntax familiar to developers
◦ All data is treated as a collection of objects

• Primarily a C# feature
◦ But appears to be gaining popularity as a standard for object-relational
mapping interfaces
◦ E.g. Stream API introduced in Java 8
◦ https://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq
◦ Py-linq project
◦ https://pypi.org/project/py-linq/

3
Background
• Lecture based largely on LINQ introduction
◦ https://docs.microsoft.com/en-us/dotnet/csharp/programming-
guide/concepts/linq/

• Also, personal experience


◦ Gained while building EdvisionApp
◦ Knowledge gained primarily from StackOverflow

• Target audience
◦ Someone like the instructor
◦ Or, small development teams
◦ Comfortable with procedural programming
◦ Less comfortable with database development

4
Introductory example
using System.Linq; 1

int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6 };

IEnumerable<int> numQuery = numbers.Where(n => n % 2 == 0); 2

foreach (int n in numQuery) 3


{

// prints 0, 2, 4, 6

Debug.WriteLine(n);

5
Example walkthrough
1. System.Linq package introduces Linq capability
◦ Adds extension methods to collections
◦ i.e. to the IEnumerable interface

2. Linq query
◦ Creates an object collection that can be queried

3. Query execution
◦ Collection can be queried
◦ Also instantiates the objects
◦ Linq uses deferred execution to optimize resources and developer convenience
◦ By default, objects are instantiated only when necessary
◦ Will be relevant when querying databases

6
Linq query details
• Consider the query
◦ IEnumerable<int> numQuery = numbers.Where(n => n % 2 == 0);

• Elements of a Linq query


1. Data source: numbers
2. Query expression: Where(n => n % 2 == 0)
3. Result: IEnumerable<int>

7
Linq query data source
• Linq can query a wide variety of data sources
◦ E.g. SQL database tables, standard collections
◦ Such as int[] numbers;

• The data source must be a queryable type


◦ Implement interface IEnumerable or IQueryable
◦ Lists, arrays etc implement IEnumerable
◦ So, can be queried directly by Linq

• Linq adds extension methods to these collections


◦ Familiar methods to database developers
◦ E.g. Where, Select, OrderBy, GroupBy

8
Linq query expressions
• Linq allows developers to perform many common operations
◦ Written as query expressions
◦ E.g. numbers.Where(n => n % 2 == 0);

• The standard syntax for Linq query expressions uses Lambda functions
◦ E.g. n => n % 2 == 0
• Read as:
◦ n : Element n in numbers
◦ => : Such that
◦ n % 2 == 0 : n satisfies this condition
• More information about lambda functions
◦ https://docs.microsoft.com/en-us/dotnet/csharp/programming-
guide/statements-expressions-operators/lambda-expressions

9
Linq query results
• Linq query results are standard object collections
◦ Can be used in the rest of the program without modification

• When used with databases using object-relational mappers


◦ Industry calls this impedance matching
◦ Program works with data stored in databases like any other object collection
◦ May like to read: http://www.odbms.org/blog/2012/05/do-we-still-have-an-impedance-
mismatch-problem-interview-with-jose-a-blakeley-and-rowan-miller/

• We will see examples when we work with relational databases


◦ Using Entity Framework as object-relational mapper

10
Common Linq operations
• Filtering
◦ Where
◦ We have seen an example

• Projecting
◦ Select

• Ordering
◦ OrderBy, OrderByDescending

• Grouping
◦ GroupBy

11
Select
• Used to generate result if different from source
◦ When using Entity Framework
◦ Also used to Include associated entities

List<string> Names = BAISCourse.Students


.Where(s => s.LastName.Contains("o"))
.Select(s => s.LastName)
.ToList();

12
Ordering
• Sort results

List<string> Names = MANCourse.Students


.Select(s => s.LastName)
.OrderByDescending(s => s)
.ToList();

13
Grouping
• Allows results to be aggregated as needed

IEnumerable<IGrouping<string, Student>> MANGrouped = MANCourse.Students


.GroupBy(s => s.FirstName);

List<Student> JohnStudents = MANGrouped.Where(g => g.Key == "John")


.SelectMany(g => g)
.ToList();

14
Exercise 1
• Return 3 times the value of each odd number in the numbers array

◦ Hint: combine filtering and projection

15
Exercise 2
• Return a list of students in a course with long last names
◦ Say length > 3

16
Exercise 3
• Return a list of all the last names of the students with last name
“John”

• Hint: Select last names from student

17
Exercise 4
• Create a question of your own

• Upload to Canvas as today’s exercise

18
References
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-
guide/concepts/linq/

19