LINQ in C#
-
Upload
basant-medhat -
Category
Technology
-
view
112 -
download
20
Transcript of LINQ in C#
![Page 1: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/1.jpg)
LINQ in C#Concepts of Programming Languages
Course 2016
![Page 2: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/2.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/3.jpg)
![Page 4: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/4.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/5.jpg)
LINQ operators
![Page 6: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/6.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/7.jpg)
LINQ OPERATORS• Filtering Operators
• Grouping Operators
• Concatenation
• Sorting Operators
• Join Operators
• Equality
• Projection Operations
![Page 8: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/8.jpg)
LINQ OPERATORSAggregation
Quantifier Operations
Partition Operations
Generation Operations
Set Operations
Conversions
Element Operators
![Page 9: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/9.jpg)
LINQ SyntaxQuery Syntax
Method Syntax
![Page 10: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/10.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/11.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/12.jpg)
![Page 13: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/13.jpg)
![Page 14: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/14.jpg)
(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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/15.jpg)
![Page 16: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/16.jpg)
![Page 17: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/17.jpg)
LINQ operators
![Page 18: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/18.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/19.jpg)
Filtering OperatorsWhere
![Page 20: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/20.jpg)
Grouping Operations The operators put data into
some groups based on a common shared attributeGroupBy
ToLookup
![Page 21: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/21.jpg)
Grouping OperationsGroupBy
![Page 22: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/22.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/23.jpg)
Concatenation Operators
Concat
![Page 24: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/24.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/25.jpg)
Sorting OperatorsOrderByy
![Page 26: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/26.jpg)
Equality Operations Compares two sentences
(enumerable ) and determine are they an exact match or not.SequenceEqual
![Page 27: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/27.jpg)
Equality OperationsSequenceEqual
![Page 28: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/28.jpg)
LINQ to Objects and Collections
![Page 29: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/29.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/30.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/31.jpg)
LINQ to Objects
![Page 32: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/32.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/33.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/34.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/35.jpg)
Joining Multiple Inputs into One Output Sequence
![Page 36: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/36.jpg)
Joining Multiple Inputs into One Output Sequence
![Page 37: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/37.jpg)
Joining Multiple Inputs into One Output Sequence
![Page 38: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/38.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/39.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/40.jpg)
Create output sequences whose elements consist of the results of operations performed on the source data.
![Page 41: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/41.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/42.jpg)
![Page 43: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/43.jpg)
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#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/44.jpg)
Any Questions?
![Page 45: LINQ in C#](https://reader033.fdocuments.in/reader033/viewer/2022061520/58cef5c71a28abab738b4eb5/html5/thumbnails/45.jpg)
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