LINQ
• Stands For: Language Integrated Query
• SQL: DQL, C#: Functions
• LINQ Method: +40 Methods
• Extension Method: For all collections that implement the interface "IEnumerable"
• Named As: LINQ Operators, existed at class Enumerable
• Categorized Into: 13 Categories
• Usage: You can use "LINQ Operators" against the data [stored in sequence] regardless of the
data store [SQL Server, MySQL, Oracle].
Sequence
• The object from a class that implements the interface "IEnumerable":
1. Local: {Static, XML Data} – L2Object, L2XML
2. Remote: L2EF
LINQ Syntax
1. Fluent Syntax
o 1.1 Call "LINQ Operators" as Static Method
o 1.2 Call "LINQ Operators" as Extension Method [Recommended]
2. Query Syntax
o Must begin with the keyword From
o Must end with Select or GroupBy
LINQ Execution Ways
• Deferred Execution
• Immediate Execution
(Elements Operators, Casting Operators, Aggregate Operators)
LINQ Categories
1. Filtration [Restriction] Operators - Where
o Get elements out of stock
o Get elements in stock and in the category of Meat/Poultry
o Get elements out of stock in the first 10 elements
2. Transformation [Projection] Operators [Select, SelectMany]
o Select Product Name
o Select Customer Name
o Select Customer Orders
o Select Product ID and Product Name
o Select Product in Stock and Apply 10% Discount on Its Price
3. Ordering Operators [Ascending, Descending, Reverse, ThenBy, ThenByDescending]
o Get products ordered by price ascending
o Get products ordered by price descending
o Get products ordered by price ascending and the number of items in stock
4. Elements Operator - Immediate Execution [Valid Only With Fluent Syntax]
o [First, Last, LastOrDefault, FirstOrDefault, ElementAt, ElementAtOrDefault, Single,
SingleOrDefault]
5. Aggregate Operators - Immediate Execution
o [Count, Sum, Min, Max, Average]
6. Casting [Conversion] Operators - Immediate Execution
o ToList: Takes the elements from the given source and returns a new List.
o ToArray: Converts the input elements in the collection to an Array.
o ToDictionary: Converts the items of a list/collection (IEnumerable<T>) to a new
dictionary object (Dictionary<TKey, TValue>).
o OfType(): Returns the elements of a specific type, ignoring other elements from the
list/collection.
o ToHashSet(): Converts the input elements in the collection to a HashSet.
7. Generation Operators - Deferred Execution
o Range: Returns a new sequence within a range.
o Repeat: Returns IEnumerable of 100 elements, each with a value of 2.
o Empty: Returns an empty IEnumerable.
o Used to create a new sequence of elements.
o Valid with Fluent Syntax only.
o The only way to call them is as static methods from the Enumerable class.
8. Set Operators [Union Family] - Deferred Execution
o Union: Without duplication.
o Concat [UnionAll]: With duplication.
o Intersect: Returns elements in the 1st sequence that exist in the 2nd sequence.
o Except: Returns elements in the 1st sequence that do not exist in the 2nd sequence.
o Distinct: Removes duplicates [Concat + Distinct => Acts as Union].
o Set Operators: Union, UnionAll, Except, Intersect.
9. Quantifier Operator - Returns Boolean
o Any: If the sequence contains at least one element, it returns True.
o All: If all elements in the sequence match the condition, it returns True.
o SequenceEqual: If two sequences are equal, it returns True.
10. Zipping Operator - ZIP
o ZIP: Produces a sequence with elements from two or three specific sequences.
11. Grouping Operators
o Get products grouped by category.
o Get products in stock grouped by category.
o Get products in stock grouped by category that contains more than 10 products.
o Get category name of products in stock that contain more than 10 products and the
number of products in each category.
12. Partitioning Operators
o Take: Takes a number of elements from the first only.
o Skip: Skips a number of elements from the first and gets the rest of the elements.
o TakeLast: Takes a number of elements from the last only.
o SkipLast: Skips a number of elements from the last and gets the rest of the elements.
o TakeWhile: Takes elements until an element that does not match the condition is found.
o SkipWhile: Skips elements until an element that does not match the condition is found.
13. Let and Into [Valid With Query Syntax Only]
o let: Continues the query with an added new range.
o into: Restarts the query by introducing a new range.