Sunteți pe pagina 1din 21

LINQ Training

Arun Kumar Mani (IWS Team)


Language-Integrated Query(LINQ)
• A query is an expression that retrieves data from a data source.
• Queries are usually expressed in a specialized query language.
Different languages have been developed over time for the various
types of data sources, for example SQL for relational databases and
XQuery for XML
• Microsoft’s query language is fully integrated and offers easy data
access from in-memory objects, databases, XML documents, and
many more
LINQ Architecture
LINQ needs a .NET framework, a revolutionary platform to have a diverse kind of
applications. A LINQ query can be written either in C# or Visual Basic conveniently
It is initially been implemented in c# 3.0.
In a LINQ query, you are always working with objects.
You use the same basic coding patterns to query and transform data in XML
documents, SQL databases, ADO.NET Datasets, .NET collections, and any other
format for which a LINQ provider is available
It Provides intellisense and compile time error checking.
Query Expression

Lambda Expression
Aggregate function
Aggregate function that specifies how two values are combined to form an
intermediate or the final result.
Restriction and Projection operators
• A restriction operator in LINQ specifies the statement should only affect rows that
meet a specified criteria
“Where” standard query operator to restrict operation category like SQL.
• The Projection operator in LINQ retrieves zero or more rows from one or more
database tables.
1. Select
2. SelectMany
select operator performs a projection on the collection to select elements from the
database.
SelectMany operator performs the sequences of values which are based on a transform
function as well as flattens them into a single sequence. (Use multiple from clauses)
Ordering Operator
• A sorting operation allows ordering the elements of a sequence on basis of a
single or more attributes
 OrderBy
 OrderByDescending
 ThenBy
 ThenByDescending
 Reverse

• ThenBy and ThenByDescending is used along with OrderBy and


OrderByDescending to sort data by more than one expression
Partition Operators
• Take - Take a specified number of elements from a sequence and skip the
remaining ones
• TakeWhile - Same as that of Take except the fact that number of elements to take
are specified by a Boolean condition
• Skip - Skips some specified number of elements within a sequence and returns
the remaining ones
• SkipWhile - Same as that of Skip with the only exception that number of
elements to skip are specified by a Boolean condition
Paging in LINQ
Conversions Operator
• ToList – Extract all of the items from the source sequence and return new List<T>
• ToArray - Extract all of the items from the source sequence and return new Array
• ToDictionary - Extract all of the items from the source sequence and return new
Dictionary
• ToLookup –Created a lookup just like a dictionary. A lookup is a collection of
key/value pairs, A dictionary cannot contains duplicate key where as a lookup can
• Cast - Performs casting of elements of a collection to a specified type, If an item
fails to convert an exception will throw
• OfType – will return only element of the specified type and the other type
elements are simply ignored and excluded from the result set.
• AsEnumerable - Returns the input typed as IEnumerable<T>
• AsQueryable - A (generic) IEnumerable is converted to a (generic) IQueryable
IEnumerable VS IQueryable
GroupBy
 The GroupBy operator takes a function that extracts a key value and returns a collection of IGrouping<Key, Values> objects, for each distinct
key value.
• The IGrouping objects can then be used to enumerate all the objects for a particular key value
Element Operator
• First – return first element in the collection if the element is empty it will throw an
exception
• FirstOrDefault – This is very similar to first except that this method does not throw an
exception when no element satisfies the condition specified by the predicate.
• Last – Very similar to first except it returns last element of the sequence
• LastOrDefault- very similar to firstOrDefault
• ElementAt – Returns an element at a specified index. If the sequence is empty or if
provide the index values is out of range throw an exception
• ElementAtOrDefault- Very similar to ElementAt, does not throw an exception
• Single – It takes a predicate and returns the element that matches the predicate. An
exception is thrown, if none or more than one element match the predicate.
• SingleOrDefault– It takes a predicate and return the element that matches the predicate.
If more than one element matches the predicate, an exception is thrown. If no element
matches the predicate, a default value is returned
• DefaultIfEmpty – return default data type value even if the specified value is empty
LINQ JOINS
• GroupJoin – Group join produces hierarchical data structure and each element
from the first collection is paired with a set of correlated element from the
second collection.
• InnerJoin - The Join operator performs an inner join on two collections, based
on matching keys for objects in each collection
• LeftOuterJoin – Matching keys and left side table all information
• CrossJoin - CrossJoin produces a result set which is the number of rows in the
first table multiplied by the number of rows in the second table
Set Operator
• Distinct -The Distinct operator removes duplicate instances of an
object from a collection. An overload of the operator takes an
equality comparer object which defines the criteria for distinctness
• Union - Union combines two collection into one collection while
removing the duplicate elements.
• Intersect - Produces the set intersection of two sequences by using
the default equality comparer to compare values.
• Except - Produces the set difference of two sequences
Generation Operator
• Range – Range operator generates a sequence of integer within a
specified range.
• Repeat - Repeat operator is used to generate a sequence that
contains one repeated value
• Empty –
• IEnumerable.Empty<int>();
• Ienumerable.Empty<string>();
Concat Operator
• The Concat operator concatenates two collections.
Sequence Equal Operator
• SequenceEqual() method is used to determine whether two sequence
are equal then it will return true or false.
Quantifiers In LINQ
Quantifiers returns true or false depending on whether if some
or all of the element is a sequence satisfy a condition.
• All – used to determine whether all the elements in a sequence
satisfy a condition.
• Any - used to determine whether any elements in a sequence satisfy
a condition.
• Contains - used to determine whether a sequence contains a
specified element

S-ar putea să vă placă și