In MVC4 stelle ich dem Benutzer Suchfeld zur Verfügung, um jeden Wert in der Tabelle zu suchen. So auf Server-Seite Generic Filter Zustand Ich bin der Umsetzung in C#LINQ-Filterimplementierung mit Ausdrücken
eine Hilfe benötigen mehrere Ausdrücke zu kombinieren einzelnen Ausdruck
Expression<Func<T, bool>>
Für Beispiel
Tabellenspalten
zu bildenMenuText, Rollenname (Role.Name Mapping), Actio nName
Jetzt, wenn Benutzer in Suchfeld für ABC eingegeben, die in einer der Zeilen in Spalten angezeigt werden können, müssen Sie filtern.
Modell
public class Menu
{
public string MenuText {get;set;}
public Role Role {get;set;}
public string ActionName {get;set;}
}
public class Role
{
public string Name {get;set;}
}
Bisher habe ich umgesetzt
/// <summary>
/// string[] properties property.Name (MenuText, ActionName), including deeper Mapping names such as (Role.Name)
/// </summary>
public static Expression<Func<T, bool>> FilterKey<T>(string filterText, params string[] properties)
{
ParameterExpression parameter = Expression.Parameter(typeof (T));
Expression[] propertyExpressions = properties.Select(
x => !string.IsNullOrEmpty(x) ? GetDeepPropertyExpression(parameter, x) : null).ToArray();
Expression<Func<T, bool>> predicate = PredicateBuilder.False<T>();
foreach (Expression expression in propertyExpressions)
{
var toLower = Expression.Call(expression, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
var like = Expression.Call(toLower, typeof(string).GetMethod("Contains"), Expression.Constant(filterText.ToLower()));
//TODO: Combine expressions to form single Expression<Func<T, bool>> expression
}
return predicate;
}
/// <summary>
/// To Get Deeper Properties such as Role.Name Expressions
/// </summary>
private static Expression GetDeepPropertyExpression(Expression initialInstance, string property)
{
Expression result = null;
foreach (string propertyName in property.Split('.'))
{
Expression instance = result ?? initialInstance;
result = Expression.Property(instance, propertyName);
}
return result;
}
Sie haben fast identische Frage 3 Mal in den letzten 24 Stunden gefragt ... – leppie