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