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);
}