blogs

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


miércoles, 23 de septiembre de 2015

Linq III

Deferred execution 
In previous post http://marisolca.blogspot.com/2015/09/linq.html we made queries that do not get really until produce the iteration on it,it is a default behavior of LINQ, but if we want to "cache" in memory   the result of the query to reuse.We will use  the standard operators as ToArray<T>(),ToList<T>(),ToDictionary<T,K>() and ToLookup<T,K,E>()
For Example,we want to get the name of employees that start with letter "A"

Our class Employee




Our method gets a list of employees


Create a test method to see the result of deferred execution 



Using the standard operators


The result of the execution





Syntax the query expressions

<Expression query>::=<FROM clause><body clause>
<FROM clause>::=FROM<element> IN<origin expression>

<body clause>::=<body clause query>*
                            <end clause query>
                            <continuation > ?

<body clause query>::= (<FROM clause>
                                     | <JOIN clause>
                                      | <JOIN-INTO clause>
                                    | <LET clause>
                                    | <WHERE clause>
                                     |<ORDERBY clause>)

<LET clause>::=LET<element>=<Expresion.selection>

<WHERE clause> ::=WHERE<expression.filter>

<JOIN clause>::=JOIN<element> IN <expresion.origin>
                        ON<Expression.key> EQUALS<expression.key>

<JOIN-INTO clause>::= JOIN<element> IN <expression.origin>
                                   ON<expression.key> EQUALS<expression.key>
                                   INTO<element>

<ORDERBY clause>::= ORDERBY<ordinations>

<ordinations>::=<ordination>| <ordinations>,<ordination>
<ordination>:=<expression.key>(ascending | descending)?

<end clause query>::=(<SELECT clause> |  <GROUPBY clause>

<SELECT clause>::=SELECT<expression.selection>
<GROUPBY clause>::=GROUP<expression.selection> BY<expression.key>

<continuation>::INTO<element><body query>

Meta-language :
* - zero or more times
(.... | ....) - alternative
? - optional element

In the following post, we will do examples the queries


miércoles, 16 de septiembre de 2015

Linq ||

I explained about LINQ in  previous post. Review this post http://marisolca.blogspot.com/2015/07/linq.html

Standard Mechanisms to apply LINQ in an arrays and generic collections(LINQ to Objects),at least there are others three technologies:
LINQ to XML:This execute integrated query on documents XML
LINQ to DataSets: This execute queries to LINQ style  against sets of typed or untyped data .
LINQ to SQL:to do possible the queries of  related database by programming language resources.

The LINQ architecture can be described to the following graphic



Query expressions: It is main mechanism by LINQ purchase life ,
It is a expression that respond to a new syntax that has been added to C# 3.0 and this can act  on any object that implements generic interface IEnumerable<T> 
For example

These are our classes








Add test methods


Using Lambda functions and extension methods

In  previous posts I explained about Lambda expression, and extension  methods, 
IEnumerable interface.
Review these post.


Using Lambda Functions




The test method shows people that satisfy the condition.  




Using Extension Methods





We can make the other way, the result is the same


The result of the execution of the two methods






In this case we have only added extension method to Where(), we also can add to Select 






 

miércoles, 9 de septiembre de 2015

Partial Classes and Methods

Partial Methods:
A partial class or struct may contain a partial method, one part of the class contains the signature of the method. An option implementation may be defined in the same part or another part.
A partial methods has its signature defined in one part of a partial type , and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks,similar to event handlers,that developers may decide to implement or not. If the developer does not supply an implementation the compiler removes the signature at compile time.

  • Signatures in both parts of the partial type must match
  • The method must return void
  • No access modifiers are allowed.Partial methods are implicitly private and therefore they can not be virtual.
  • A partial method must be declared within  a partial class  or partial struct
  • The partial methods can be static, they can also be extension methods if they are part of a static class.
  • The partial methods can have any amount of parameters; These can be ref, but it is not out

 The result of the compiler will be:

  • The compiler does not find an implementation of the method PartialMethod1(), so that eliminate the method of the class PartialClass as if it had never existed
  • The compiler deleted the call to  Partial method(PartialMethod1()) that is in the Method  publicMethod(). if we partial method does not have defined a body,the compiler delete this method.
  • PartialMethod2() has defined a body so that this method is present in this class


Partial Classes:
When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time


  • The code can be added to the class without having to recreate the source file.
  • To split a class definition,use the partial keyword modifier.
  • The partial keyword indicates that other parts of the class,struct or interface can be defined in the namespace,All the parts must use the partial keyword,they must have the same accessibility,.
  • If any part is declared abstract, then the whole type is considered abstract, if any part is declared sealed,then the whole type is considered sealed.
  • The partial modifier is not available on delegate or enumeration declarations


Utility of partial methods and classes
It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

Restrictions 


  • All partial-type must be defined in the same assembly and the same module.
  • The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
  • All partial-type definitions meant to be parts of the same type must be modified with partial
  • The partial modifier can only appear immediately before the keywords class, struct, or interface


For example