Linq si Dynamic Query

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 ?

3 thoughts on “Linq si Dynamic Query

Leave a Reply

Your email address will not be published. Required fields are marked *