PDA

View Full Version : [C# / .net 4.0]Query dinamiche


RaouL_BennetH
15-10-2012, 23:58
Buonasera a tutti.

Sto cercando di implementare un tipo di ricerca dinamica basata sul metodo "Contains":


public static Expression<Func<T, bool>> DynamicExpression(string propertyName, string filter)
{
//concettualmente
ParameterExpression param = Expression.Parameter(typeof(T), "test");
Expression absProperty = Expression.Property(param, propertyName);
Expression dest = Expression.Constant(filter);
Expression customContains = Expression.Call(absProperty, "Contains", null, dest);
Expression<Func<T, bool>> exp = Expression.Lambda<Func<T, bool>>(customContains, param);
return exp;
}



Ora, se ho una fonte di questo tipo:

IQueryable result = from r in blah.Where(DynamicExpression("colonna", "valore")
select new
{
//blah
};


supponendo che l'oggetto sia :

id
colonna
colonna2
colonna3
OggettoX(entità che ha una relazione 1-1 o 1-n)...leggasi NavigationProperty

fin quando cerco sulle "colonne" funziona tutto.
Ovviamente quando cerco 'anche' sull'oggettoX giustamente
mi dice che non esiste una proprietà associata con quel nome.

Per spiegarmi meglio:

IQueryable result = from r in ctx.Parent.Where(dynamic("category", "blah"))
select new
{
id = r.id,
category = r.Child.category,
columnLol = r.lol
};

l'errore che ottengo è:


Instance property 'category' is not defined for type 'Parent'