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 22, 2008

Linq si Dynamic Query
Va rog sa folositi acest URL NOU de la
http://serviciipeweb.ro/iafblog/

Am downloadat sample-urile de C# si, cu generarea automata in cap, m-am apucat de Dynamic Query.
Exemplul e relativ bun:

Northwind db = new Northwind(connString);
db.Log = Console.Out;
Console.WriteLine("before");
var query =
db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("New(CompanyName as Name, Phone)");


Console.WriteLine(query);
Console.ReadLine();


Dar cum vad rezultatele ?
Prima incercare a fost simpla:

foreach (var c in query)
{
Console.WriteLine(c.ToString());
}


Ma rog... destul de bine... Dar pentru o aplicatie adevarata as avea nevoie sa vad numele si telefonul...

Asa ca m-am dus in DynamicClass si am adaugat:

private PropertyInfo[] _pi;
public PropertyInfo[] Properties
{
get
{
if (_pi == null)
{
_pi = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
}
return _pi;
}
}
public object GetValue(string PropName)
{
PropertyInfo pi= Properties.Single<PropertyInfo>(c => c.Name == PropName);
return pi.GetValue(this, null);
}

In felul acesta am putut scrie in main:

Northwind db = new Northwind(connString);
db.Log = Console.Out;
Console.WriteLine("before");
var query =
db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("New(CompanyName as Name, Phone)");

Console.WriteLine("after");
foreach (DynamicClass c in query)
{

Console.WriteLine(c.GetValue("Name"));
Console.WriteLine(c.GetValue("Phone"));
Console.WriteLine("-------------------");
}
Console.WriteLine(query);
Console.ReadLine();



Problema rezolvata....
Ati facut Linq ? Si, daca da, cum ati (fi) rezolvat problema ?

Post page: http://serviciipeweb.ro/iafblog/2008/01/22/Linq+Si+Dynamic+Query.aspx

Weblog post by 'admin' on 'Linq si Dynamic Query'

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/