0% found this document useful (0 votes)
18 views26 pages

Lecture 5 - LINQ

LINQ (Language-Integrated Query) is a feature in C# and VB.NET that allows for a uniform querying syntax to retrieve data from various sources, including objects, databases, and XML. It integrates query capabilities directly into the language, providing a consistent experience and enabling an object-oriented approach to handle results. LINQ includes various query operators for filtering, sorting, grouping, and more, and can be used with both in-memory collections and external databases through LINQ to SQL.

Uploaded by

m09221011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Lecture 5 - LINQ

LINQ (Language-Integrated Query) is a feature in C# and VB.NET that allows for a uniform querying syntax to retrieve data from various sources, including objects, databases, and XML. It integrates query capabilities directly into the language, providing a consistent experience and enabling an object-oriented approach to handle results. LINQ includes various query operators for filtering, sorting, grouping, and more, and can be used with both in-memory collections and external databases through LINQ to SQL.

Uploaded by

m09221011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

LINQ

Abdulmottaleb Elabour

1
What is LINQ?
• Language-Integrated Query (LINQ) is a powerful set of technologies based on the integration of query
capabilities directly into the C# language. LINQ Queries are the first-class language construct in C# .NET, just
like classes, methods, events. The LINQ provides a consistent query experience to query objects (LINQ to
Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

• LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different
sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming
languages and databases, as well as providing a single querying interface for different types of data sources.

• For example, SQL is a Structured Query Language used to save and retrieve data from a database. In the same
way, LINQ is a structured query syntax built in C# and VB.NET to retrieve data from different types of data
sources such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server and other
databases.
2
‫عبد المطلب العبور‬
‫?‪What is LINQ‬‬

‫‪3‬‬
‫عبد المطلب العبور‬
What is LINQ?
LINQ queries return results as objects. It enables you to uses object-oriented approach on the result set
and not to worry about transforming different formats of results into objects.

4
‫عبد المطلب العبور‬
What is LINQ?

Output:
Fatima
ali
aisha

5
‫عبد المطلب العبور‬
LINQ Query Syntax

6
‫عبد المطلب العبور‬
C# - Func Delegate
• C# includes built-in generic delegate types Func and Action, so that you don't need to define custom
delegates manually in most cases.
• Func is a generic delegate included in the System namespace. It has zero or more input parameters
and one out parameter. The last parameter is considered as an out parameter.
• The Func delegate that takes one input parameter and one out parameter is defined in the System
namespace, as shown below:

7
‫عبد المطلب العبور‬
C# - Func Delegate
Output:
20
4

8
‫عبد المطلب العبور‬
LINQ Method Syntax

Output: Method syntax (also known as fluent syntax) uses extension methods included in the
C# Tutorials Enumerable or Queryable static class, similar to how you would call the extension method
VB.NET Tutorials of any class.
Python Tutorials
9
‫عبد المطلب العبور‬
LINQ Method Syntax
The following figure illustrates the structure of LINQ method syntax.

10
‫عبد المطلب العبور‬
LINQ Method Syntax

11
‫عبد المطلب العبور‬
Standard Query Operators
Classification Standard Query Operators
Filtering Where, OfType
Sorting OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse

Grouping GroupBy, ToLookup


Join GroupJoin, Join
Projection Select, SelectMany
Aggregation Aggregate, Average, Count, LongCount, Max, Min, Sum

Quantifiers All, Any, Contains


Elements ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
Set Distinct, Except, Intersect, Union
Partitioning Skip, SkipWhile, Take, TakeWhile
Concatenation Concat
Equality SequenceEqual
Generation DefaultEmpty, Empty, Range, Repeat
Conversion AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList

12
‫عبد المطلب العبور‬
Chaining Query Operators
Output:
JAY
MARY
HARRY

13
‫عبد المطلب العبور‬
Chaining Query Operators
We can construct the identical query progressively, as follows:
// You must import the System.Linq namespace for this to compile:

Output:
JAY
MARY
HARRY

14
‫عبد المطلب العبور‬
Natural Ordering
The original ordering of elements within an input sequence is significant in LINQ.
Some query operators rely on this ordering, such as Take, Skip, and Reverse.

15
‫عبد المطلب العبور‬
Other Operators
Not all query operators return a sequence. The element operators extract one element
from the input sequence; examples are First, Last, and ElementAt:

16
‫عبد المطلب العبور‬
Other Operators
Some query operators accept two input sequences. Examples are Concat, which
appends one sequence to another, and Union, which does the same but with duplicates
removed:

17
‫عبد المطلب العبور‬
Chaining Decorators
Chaining query operators creates a layering of decorators. Consider the following
query:

18
‫عبد المطلب العبور‬
Chaining Decorators

19
‫عبد المطلب العبور‬
How Queries Are Executed

Output:
30
50
20
‫عبد المطلب العبور‬
LINQ to SQL
• LINQ to SQL is a powerful feature in C# that allows developers to interact with relational
databases using LINQ queries.
• You must include System.Data.Linq when you want to use LINQ to SQL.

21
‫عبد المطلب العبور‬
LINQ to SQL

Output:
Course ID: 1
Course Name: C#
Units: 3
Course ID: 4
Course Name: Linear Algebra
Units: 2
Course ID: 6
Course Name: Networks
Units: 3
22
‫عبد المطلب العبور‬
LINQ to SQL

Course ID: 1
Course Name: C#
Units: 3
Course ID: 6
Course Name: Networks
Units: 3
23
‫عبد المطلب العبور‬
LINQ to SQL
Use IQueryable when querying external sources (like a
database) to avoid loading too much data.

Course ID: 1
Course Name: C#
Units: 3
Course ID: 6
Course Name: Networks
Units: 3 24
‫عبد المطلب العبور‬
IQueryable VS IEnumerable
Feature IEnumerable IQueryable

Defined In System.Collections System.Linq

When Query Executes Immediately (in-memory) Deferred (translated to SQL, etc.)

In-memory collections (e.g.


Data Source External sources (e.g. databases)
List<>)

Where Filter Executes On the client (C#) On the server (SQL)

Use Case LINQ to Objects LINQ to SQL / Entity Framework

Less efficient with large More efficient (fetches only what you
Performance
datasets need)

25
‫عبد المطلب العبور‬
Insert new row to database

26
‫عبد المطلب العبور‬

You might also like