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 ?
scuze … Adresa era de fapt:
http://msdn2.microsoft.com/en-us/vcsharp/bb330936.aspx
Apasati, la C# pe English ,
http://msdn2.microsoft.com/vcsharp/bb894665.aspx
faza aia cu where dat ca si string nu mai merge pt versiune actuala … caut si nu gasesc cu sa fac un where dynamic.
Draga Mitza
"Nu merge" nu e o fraza de programator.
Mai degraba spune ce ai incercat si ce eroare iti da…