blogs

jueves, 29 de octubre de 2015

Standard query operators

You need to review the previous posts for understanding more about queries 
http://marisolca.blogspot.com/2015/07/linq.html
 http://marisolca.blogspot.com/2015/10/linq-v-example-of-queries.html 

Type Parameters:
TSource: The type of the elements of source.
TResult: The type of the value returned by selector.

Parameters:
source: A sequence of values to invoke a transform function on.
selector: A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

Returns:

An System.Collections.Generic.IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.


LINQ pattern operators
For this example we will use these classes







Data




Where():Filter the original sequence in base  on predicate logic.Return an IEnumerable(sequence).If any argument is null , ArgumentNullException exception launches.
The method has two method overloading


For Example:








select()/selectMany():Projection of original sequence in other base on one tranformation function

Method overloading
For example:




Unlike Select () standard query operator SelectMany () is normally used in consultations with nested generators, and aims to make a projection of one to many:

Method overloading


Note that it was necessary to assign explicit names to properties resulting anonymous type, because otherwise it would be with two properties called Name, which is obviously unacceptable.








OrderBy/ThenBy(): Sort ascending of the original sequence base on calculation functions of the sort key.
OrderBYDescending()/thenByDescending(): Sort descending of the original sequence base to calculation functions of the sort key

Method overloading









GroupBy():Create groups from original sequence base on calculation function the grouping key

Method overloading

public static IEnumerable<IGrouping<TKey, TSource>>GroupBy<TSource, TKey>(this IEnumerable<TSource> source,Func<TSource, TKey> keySelector);
public static IEnumerable<IGrouping<TKey, TSource>>GroupBy<TSource, TKey>(this IEnumerable<TSource> source,Func<TSource, TKey> keySelector,IEqualityComparer<TKey> comparer);
public static IEnumerable<IGrouping<TKey, TElement>>GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,Func<TSource, TKey> keySelector,Func<TSource, TElement> elementSelector);
public static IEnumerable<IGrouping<TKey, TElement>>GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,Func<TSource, TKey> keySelector,Func<TSource, TElement> elementSelector,IEqualityComparer<TKey> comparer);

IGrouping<TKey,TElement> return any of above four operators
public interface IGrouping<TKey,TElement>;IEnumerable<TElement>
{
  TKey key{get;}
}
KeySelector parameters represent a function that calculates, each element sequence input, the grouping key value that determine to which group belongs the element
elementSelector represent a function of mapping  that determine what object is associated  to a group.





Join(): internal meeting of original sequence and a second sequence base on calculation functions of the key  meeting for each  of the sequences.

Method overloading
public static IEnumerable<TResult>Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,IEnumerable<TInner> inner,Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,Func<TOuter, TInner, TResult> resultSelector);

public static IEnumerable<TResult>Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,IEnumerable<TInner> inner,Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,Func<TOuter, TInner, TResult> resultSelector,
IEqualityComparer<TKey> comparer);

the arguments outerKeySelector e innerKeySelector  are functions that extracting the values of the key meeting to each of the elements the input sequence (outer) and the second sequence (inner), respectively.
resultSelector is  the function responsible for generating  elements  of result sequence from each pair of elements of the sequence whose values match







No special query operators to deploy meetings outside the left or right, but these can be implemented easily by GroupJoin () operator discussed below.

GroupJoin(): 
grouping meeting of original sequence and second sequence base on calculation functions of the key meeting for each of the sequence
resultSelector is  the function responsible for generating  elements  of result sequence from each pair of elements of the sequence whose values match.

Method overloading
public static IEnumerable<TResult>GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,IEnumerable<TInner> inner,Func<TOuter, TKey> outerKeySelector,Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector);

public static IEnumerable<TResult>GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,IEnumerable<TInner> inner,Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,IEqualityComparer<TKey> comparer);

the arguments outerKeySelector e innerKeySelector  are functions that extracting the values of the key meeting to each of the elements the input sequence (outer) and the second sequence (inner), respectively.





miércoles, 14 de octubre de 2015

Linq V: Example of Queries







Left outer join
Left outer join is a join in which each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection.In LINQ we can use it to perform a left outer join by calling the DefaultEmpty<TSource> method on the result of a group join
DefaultEmpty<TSource> method: Returns the elements of the specify sequence or the type parameter's default value in a singleton collection if the sequence is empty.
For example.
Add a new employee and delete Country code of Antonia






The notion of left or right is relative.If we are interested in getting a match which appeared all people, even thought they did not have an associated country, we could change the order of the operands of the match in the previous query.

it deleted country code of Sole and was added it to Antonia




                                             



Symmetrical external match




LET clause
In a query expression, it is sometimes useful to store the result of sub-expression in order to use it in subsequent clauses.We can do this with LET keyword, which creates a new range variable and initializes it with the result of the expression us supply.




Other examples:
We want to get people whose age is greater  than or equal that middle ages of the group. If we use LET key, it will be done with a single query.

miércoles, 30 de septiembre de 2015

Linq IV: Example of Queries

Prerequisite, you need to see these posts
http://marisolca.blogspot.com/2015/09/linq.html
http://marisolca.blogspot.com/2015/09/linq-iii.html
If you do not know about the Lambda expression , you need to review this post  http://marisolca.blogspot.com/2015/07/lambda-expression.html

These are our two methods that return lists of employees and countries.







  • Groups




We make the previous example of other way




JOIN clause

JOIN with ORDER BY clause




JOIN with GROUP BY clause



Resulting the other way the previous example 






  • INTO clause

It is using for two  different objects in a query expression.

  1. First is that makes possible implement the continuations.
  2. Second ,it allows  expression grouped joins.


1:INTO clause as continuation
It allows perform in cascade two query expressions






2:INTO clause as grouped joins

Produce a sequence in which each element of external sequence is paired  with the group of elements of the internal sequence whose key values comparison matches with the key value comparison of the element the external sequence.


JOIN INTO  clause is translated in a call to GROUPJOIN() standard operator  

We make the previous example of other way


In the next post, I will explain more about queries