2010-10-13 18 views
6

Ho un metodo di estensione Linq per filtrare dinamicamente le query di Linq utilizzando i valori stringa. Ad esempio: query.WhereHelper("columName", ">", 1). Potrei usare molti diversi operatori di filtri come GreaterThan o NotEqual ecc. Ma non "Mi piace". Non c'è Expression.Like o Expression.StartsWith ecc. Come posso implementare l'operatore Like al mio albero Expression? Ecco il mio codice:Operatore simile a Expression Tree

public static IQueryable<T> WhereHelper<T>(this IQueryable<T> source, string columnName, object value, string filterType) 
{ 
    ParameterExpression table = Expression.Parameter(typeof(T), ""); 
    Expression column = Expression.PropertyOrField(table, columnName); 
    Expression valueExpression = Expression.Convert(Expression.Constant(value), column.Type); 
    Expression where = null; 

    switch (filterType) 
    { 
     case "<": 
      where = Expression.LessThan(column, valueExpression); 
      break; 
     case "<=": 
      where = Expression.LessThanOrEqual(column, valueExpression); 
      break; 
     case "=": 
      where = Expression.Equal(column, valueExpression); 
      break; 
     case ">": 
      where = Expression.GreaterThan(column, valueExpression; 
      break; 
     case ">=": 
      where = Expression.GreaterThanOrEqual(column, valueExpression); 
      break; 
     case "<>": 
      where = Expression.NotEqual(column, valueExpression); 
      break; 
    } 

    Expression lambda = Expression.Lambda(where, new ParameterExpression[] { table }); 

    Type[] exprArgTypes = { source.ElementType }; 

    MethodCallExpression methodCall = Expression.Call(typeof(Queryable), 
                 "Where", 
                 exprArgTypes, 
                 source.Expression, 
                 lambda); 

    return (IQueryable<T>)source.Provider.CreateQuery<T>(methodCall); 

risposta

7

Si potrebbe utilizzare Expression.Call con i string.StartsWith, string.Contains, string.EndsWith metodi ecc. È per il codice che consuma per tradurlo in TSQL. Nota che per LINQ-to-SQL ci sono anche alcune funzioni di supporto aggiuntive qui, ma non con EF.

+1

Le "funzioni di supporto" Voglio dire sono [ 'SqlMethods'] (http://msdn.microsoft.com/en-us/library/system. data.linq.sqlclient.sqlmethods.aspx) - in particolare 'Mi piace()' –

+1

Il commento su EF non è corretto. C'è [EntityFunctions] (http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.aspx) che è analogo a 'SqlMethods' per L2S. Tuttavia, funzioni come 'EndsWith' e' Contains' sono supportate nativamente in EF e non richiedono tali helper. –

+0

@Craig grazie; Non lo sapevo. –

6

È possibile definire COME espressione come segue,

var propertyName = "Firstname"; 
var propertyValue= "xxxx"; 

MethodInfo refmethod = typeof(string).GetMethod("Contains", new[] { typeof(string) }); 
var parameter = Expression.Parameter(typeof(T), "type"); 
var property = Expression.Property(parameter, propertyName); 
var value = Expression.Constant(propertyValue, typeof(string)); 
var containsMethodExp = Expression.Call(property, refmethod, value); 
+0

Testato e funziona correttamente Hai salvato la mia giornata! Grazie! – GiveEmTheBoot