In principal, despre .NET, VB6. C#,VB.NET, ASP.NET Apoi despre programare si multe alte chestii ... de programare.

Subscribe RSS   Mini tools List

marți, ianuarie 29, 2008

linq si defered execution
Va rog sa folositi acest URL NOU de la
http://serviciipeweb.ro/iafblog/

oarte interesante rezultatele - si cum te poti insela

The following sample shows how query execution is deferred until the query is enumerated at a foreach statement.

public void Linq99() {
// Sequence operators form first-class queries that
// are not executed until you enumerate over them.

int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int i = 0;
var q =
from n in numbers
select ++i;

Console.WriteLine("i = {0}", i);
// Note, the local variable 'i' is not incremented
// until each element is evaluated (as a side-effect):
foreach (var v in q) {
Console.WriteLine("v = {0}, i = {1}", v, i);
}
}


Ce valoare are i cind se executa Console.WriteLine("i = {0}", i);  ? Raspuns : 0!

Rezultat :

i = 0
v = 1, i = 1
v = 2, i = 2
v = 3, i = 3
v = 4, i = 4
v = 5, i = 5
v = 6, i = 6
v = 7, i = 7
v = 8, i = 8
v = 9, i = 9
v = 10, i = 10

Al doilea exemplu :
The following sample shows how queries can be executed immediately with operators such as ToList().

public void Linq100() {
// Methods like ToList() cause the query to be
// executed immediately, caching the results.

int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

int i = 0;
var q = (
from n in numbers
select ++i )
.ToList();

Console.WriteLine("i = {0}", i);
// The local variable i has already been fully
// incremented before we iterate the results:
foreach (var v in q) {
Console.WriteLine("v = {0}, i = {1}", v, i);
}
}


Ce valoare are i cind se executa Console.WriteLine("i = {0}", i);  ? Raspuns : 10!
Rezultat :
i = 10
v = 1, i = 10
v = 2, i = 10
v = 3, i = 10
v = 4, i = 10
v = 5, i = 10
v = 6, i = 10
v = 7, i = 10
v = 8, i = 10
v = 9, i = 10
v = 10, i = 10

De asemenea m-a impresionat CreateDatabase();  - desi cred ca era mai impresionant pe generics...
Abia astept versiunea pentru access - sa vezi atunci export import...

La fel , m-a impresionat inheritance cu ajutorul lui [InheritanceMapping(Code="Shipper", Type=typeof(ShipperContact))] si 
[Column(Storage="_ContactType", DbType="NVarChar(50)", IsDiscriminator=true)]

public void LinqToSqlInheritance03()
{
    var cons = from c in db.Contacts
               where c is ShipperContact
               select c;

    ObjectDumper.Write(cons, 0);
}

Post page: http://serviciipeweb.ro/iafblog/2008/01/29/linq+Si+Defered+Execution.aspx

Weblog post by 'admin' on 'linq si defered execution'

Categories:.NET;LINQ;programare

0 Comments:

Trimiteți un comentariu

Links to this post:

Creați un link

<< Home

 
Acest blog s-a mutat la http://www.serviciipeweb.ro/iafblog/