Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Tuesday, April 26, 2011

LINQ at Glance - Part 3

Standard Query Operators

Sequence-->Sequence
Filtering : IEnumerable<TSource>?IEnumerable<TSource>
Returns a subset of the original elements
Where, Take, TakeWhile, Skip, SkipWhile, Distinct
Projecting:
IEnumerable<TSource>?IEnumerable<TResult>
Transfors each element with Lambda Expression.
Select, SelectMany
Joining:IEnumerable<TOuter>, IEnumerable<TInner>?IEnumerable<TResult>
Meshes the elements of once sequence to another
Join, GroupJoin
Ordering:
IEnumerable<TSource>?IOrderedEnumerable<TSource>
Returnes Reordering the sequence
OrderBy, ThenBy, Reverse
Grouping:
IEnumerable<TSource>?IEnumerable<IGrouping<TSource,TElement>>
Groups the sequence into sub sequences
GroupBY
Set operators:IEnumerable<TSource>, IEnumerable<TSource>?IEnumerable<TSource>
Takes two sequences of same type and returns commanality,sum or difference
Concat, Union, Intersect, Except
Zip operator:
IEnumerable<TFirst>, IEnumerable<TSecond>?IEnumerable<TResult>
Enumerate each elemement in step and apply function over each element pair.
Conversion methods: Import:IEnumerable?IEnumerable<TResult>
OfType, Cast
Conversion methods: Export
IEnumerable<TSource>?An array, list, dictionary, lookup, or sequence
ToArray, ToList, ToDictionary, ToLookup, AsEnumerable, AsQueryable

Sequence--> Element or ScalarElement operators:
IEnumerable<TSource>?TSource
First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault,
ElementAt, ElementAtOrDefault, DefaultIfEmpty
Aggregation methods:IEnumerable<TSource>?scalar
Aggregate, Average, Count, LongCount, Sum, Max, Min
Quantifiers:
IEnumerable<TSource>?bool
All, Any, Contains, SequenceEqual

Nothing-->SequenceGeneration methods
void?IEnumerable<TResult>
Manufactures a simple sequence.
Empty, Range, Repeat

Monday, April 25, 2011

LINQ at a Glance - Part 2

Intepreted Queries
-------------------
Interpreted Queries are descriptive
They operate over sequences that implement IQueryable<> interface.
IQueryable is an extension of IEnumerable with additional methods for constructing Expression Trees.
They resolve to query operators defined in Queryable class.
They emit expression trees that gets interpreted in runtime.
LINQ to SQL and Entity Framework implement IQueryable interface.
Using AsQueryable extension method it is possible to generate IQueryable Wrapper around IEnumerable<> sequence.
Execution of Interpreted Queries
 Interpreted Queries follow a differed execution model.
 Under the covers, interpreted queries differ from local queries in how they execute.
 When you enumerate over an interpreted query, the outermost sequence runs a program that traverses the entire expression tree, processing it as a unit.
 In case of LINQ to SQL, LINQ to SQL translates the expression tree to a SQL statement, which it
 then executes, yielding the results as a sequence.
Compiling Expression Trees - One can convert an expression tree to a delegete using Compile() method.
The AsQueryable operator lets you write whole queries that can run over either local or remote sequences

LINQ at a Glance - Part 1


Basic unit of data in LINQ are sequences and elements.

sequences are enumerable values and elements are items in the sequence.

Local Queries
local sequence -  Queries that operate over local sequences are called local queries or LINQ-to-objects queries.
query operator - Query operator is a method that transforms the sequence.
standard query operators -  Where Select etc.

Data can be queried in following ways using LINQ

Fluent Syntax
Chaining Query Operators
Use Lambda Expressions

Query Expressions/Query Syntax
Query Expression always start with from keyword and ends with select or group keyword.
Range Variables --> The identified immediately following the from keyword syntax is called range variable.
Query expression allows us to introduce new range variables using let into and additional from caluse