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/
• 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
// 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);
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;
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
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
12
Ordering
• Sort results
13
Grouping
• Allows results to be aggregated as needed
14
Exercise 1
• Return 3 times the value of each odd number in the numbers array
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”
17
Exercise 4
• Create a question of your own
18
References
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-
guide/concepts/linq/
19