45
LINQ in C# Concepts of Programming Languages Course 2016

LINQ in C#

Embed Size (px)

Citation preview

Page 1: LINQ in C#

LINQ in C#Concepts of Programming Languages

Course 2016

Page 2: LINQ in C#

What’s LINQ?Language Integrated Query

LINQ is a query syntax built in C# and VB.NET used to save and retrieve data from different types of data sources like an Object Collection, SQL server database, XML, web service etc.

Page 3: LINQ in C#
Page 4: LINQ in C#

Advantages• Syntax highlighting

• Easy debugging

• Extensible that means it is possible to query new data source types.

• Facility of joining several data sources in a single query

• Easy transformation ( like transforming SQL data to XML data.)

Page 5: LINQ in C#

LINQ operators

Page 6: LINQ in C#

LINQ OPERATORS• Known expressions used for specific purpose

• Operators offer a range of query capabilities like filtering, sorting, projection, aggregation, etc.

Page 7: LINQ in C#

LINQ OPERATORS• Filtering Operators

• Grouping Operators

• Concatenation

• Sorting Operators

• Join Operators

• Equality

• Projection Operations

Page 8: LINQ in C#

LINQ OPERATORSAggregation

Quantifier Operations

Partition Operations

Generation Operations

Set Operations

Conversions

Element Operators

Page 9: LINQ in C#

LINQ SyntaxQuery Syntax

Method Syntax

Page 10: LINQ in C#

Query Syntax• Query syntax is similar to SQL (Structured Query Language) for the

database.

• It is defined within the C#

• Query Syntax starts with from clause and can be end with Select or GroupBy clause.

Page 11: LINQ in C#

Query SyntaxImplicitly typed variable - var can be used to hold the result of the

LINQ query.

We use various operators like filtering, joining, grouping, sorting operators to construct the desired result.

Page 12: LINQ in C#
Page 13: LINQ in C#
Page 14: LINQ in C#

(lamda) Method SyntaxMethod Syntax is like calling extension method already included.

Implicitly typed variable - var can be used to hold the result of the LINQ query.

Page 15: LINQ in C#
Page 16: LINQ in C#
Page 17: LINQ in C#

LINQ operators

Page 18: LINQ in C#

Filtering Operators

Filtering is an operation to restrict the result set such that it has only selected elements satisfying a particular condition.

WhereOfType

Page 19: LINQ in C#

Filtering OperatorsWhere

Page 20: LINQ in C#

Grouping Operations The operators put data into

some groups based on a common shared attributeGroupBy

ToLookup

Page 21: LINQ in C#

Grouping OperationsGroupBy

Page 22: LINQ in C#

Concatenation

Operations

Performs concatenation of two sequences and is quite similar to the Union operator in terms of its operation except of the fact that this does not remove duplicates.Concat

Page 23: LINQ in C#

Concatenation Operators

Concat

Page 24: LINQ in C#

Sorting Operators A sorting operation allows

ordering the elements of a sequence on basis of a single or more attributes.OrderBy,OrderByDescending,

Page 25: LINQ in C#

Sorting OperatorsOrderByy

Page 26: LINQ in C#

Equality Operations Compares two sentences

(enumerable ) and determine are they an exact match or not.SequenceEqual

Page 27: LINQ in C#

Equality OperationsSequenceEqual

Page 28: LINQ in C#

LINQ to Objects and Collections

Page 29: LINQ in C#

LINQ to ObjectsLINQ to Objects offers a fresh approach to collections

Before we had to write long coding (foreach loops of much complexity) for retrieval of data from a collection

Now we write declarative code which clearly describes the desired data that is required to retrieve.

Page 30: LINQ in C#

LINQ to ObjectsThere are also many advantages of LINQ to Objects over traditional like

• Readability

• Powerful filtering

• Capability of grouping

• Enhanced ordering with minimal application coding.

Page 31: LINQ in C#

LINQ to Objects

Page 32: LINQ in C#

LINQ to ObjectsIn the example, an array of strings (tools) is used as the collection of objects

Queried using LINQ to Objects.The output will be ..

Tablesaw

Bandsaw

Planer

Jointer

Drill

Sander

Page 33: LINQ in C#

Data Source Transformation(LINQ) is not only about retrieving data.

We can use a source sequence as input and modify it in many ways to create a new output sequence.

Page 34: LINQ in C#

Data Source TransformationWe Can ...

• Merge multiple input sequences into a single output sequence that has a new type.

• Create output sequences whose elements consist of only one or several properties of each element in the source sequence.

• Create output sequences whose elements consist of the results of operations performed on the source data.

• Create output sequences in a different format. For example, you can transform data from SQL rows or text files into XML.

Page 35: LINQ in C#

Joining Multiple Inputs into One Output Sequence

Page 36: LINQ in C#

Joining Multiple Inputs into One Output Sequence

Page 37: LINQ in C#

Joining Multiple Inputs into One Output Sequence

Page 38: LINQ in C#

Joining Multiple Inputs into One Output Sequence

Output will be…

The following students and teachers live in Seattle: Omelchenko Beebe

Page 39: LINQ in C#

Create output sequences whose elements consist of only one or several properties of each element in the source sequence.Select one member of the source element

Create elements that contain more than one property from the source element

Page 40: LINQ in C#

Create output sequences whose elements consist of the results of operations performed on the source data.

Page 41: LINQ in C#

Create output sequences in a different format.We you can transform data from SQL rows or text files

into XML.

Page 42: LINQ in C#
Page 43: LINQ in C#

Resourceshttp://www.tutorialspoint.com/linq/

https://msdn.microsoft.com/en-us/library/mt693042.aspx

https://en.wikipedia.org/wiki/Language_Integrated_Query

Page 44: LINQ in C#

Any Questions?

Page 45: LINQ in C#

Thank You!LINQ in C# PresentationConcepts of Programming Languages Course 2016Faculty of Computers and Information - Cairo UniversityPresented By:Basant Mohamed Medhat - 20130102Hanan Shaban Abdelmohsen - 20130113Hager Gamal Hassan - 20130282