我有一个使用 Kendo Grid 的 MVC 站点,我正在尝试实现动态过滤器。我显示的数据包含几个一对多表。比如我有一排人,每个人可以有0个或者多个Item分配给他们。我在网格中显示一个扁平化的列表:
Bob | Item 1, Item 2
Jane | Item 3
如果我要在 Items 列上硬编码过滤器,它看起来像:
people.Where(p=> p.Items.Any(i=>i.name.contains("Item 1"))).ToList()
我想想出一种构建表达式树的通用方法,这样我就可以过滤不同的一对多字段,还可以执行不同的比较(例如包含、开始、等于等)。理想情况下,我会有一个具有以下语法的扩展方法:
public static IQueryable<TEntity> Where( this IQueryable<TEntity> source, string tableName, string fieldName, string comparisonOperator, string searchVal) where TEntity : class
然后我可以查询多个一对多表:
if(searchOnItems)
persons = persons.Where("Items", "name", "Contains", "item 1);
if(searchOnOtherTableName)
persons = persons.Where("OtherTableName", "name", "Equals", "otherSearchValue);
persons.ToList();
我正在尝试使用 LINQ to Entities string based dynamic OrderBy作为起点,因为概念相似,但我不知道如何修改 GenerateSelector 方法。任何想法将不胜感激。
编辑 - 我的代码在一个封闭的网络上,所以我会尽最大努力复制我正在尝试的东西。这是我试图修改的代码。评论区是我卡住的地方。上面调用“Where”扩展方法的例子仍然有效。
public static IQueryable<TEntity> Where<TEntity>(this IQueryable<TEntity> source, string tableName, string fieldName, string comparisonOperator, string searchVal) where TEntity : class
{
MethodCallExpression resultExp = GenerateMethodCall<TEntity>(source, "Where", tableName, fieldName, comparisonOperator, searchVal);
return source.Provider.CreateQuery<TEntity>(resultExp) as IOrderedQueryable<TEntity>;
}
private static MethodCallExpression GenerateMethodCall<TEntity>(IQueryable<TEntity> source, string methodName, string tableName, String fieldName, string comparisonOperator, string searchVal) where TEntity : class
{
Type type = typeof(TEntity);
Type selectorResultType;
LambdaExpression selector = GenerateSelector<TEntity>(tableName, fieldName, comparisonOperator, searchVal, out selectorResultType);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName,
new Type[] { type, selectorResultType },
source.Expression, Expression.Quote(selector));
return resultExp;
}
private static LambdaExpression GenerateSelector<TEntity>(string tableName, String fieldName, string comparisonOperator, string searchVal, out Type resultType) where TEntity : class
{
// Create a parameter to pass into the Lambda expression (Entity => Entity.OrderByField).
var parameter = Expression.Parameter(typeof(TEntity), "Entity");
PropertyInfo property = typeof(TEntity).GetProperty(tableName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);;
Expression propertyAccess = Expression.MakeMemberAccess(parameter, property);;
/************************************************/
//property is now "TEntity.tableName"
//how do I go another step further so it becomes "TEntity.tableName.comparisonOperator(searchVal)"
/************************************************/
resultType = property.PropertyType;
// Create the order by expression.
return Expression.Lambda(propertyAccess, parameter);
}
最佳答案
I'm attempting to use LINQ to Entities string based dynamic OrderBy as a starting point since the concept is similar, but I can't figure out how to modify the GenerateSelector method.
期望 selector 的方法之间存在显着差异,例如 Select
、OrderBy
、ThenBy
等。期望 predicate 的方法,如 Where
、Any
等。后者不能使用上面的 GenerateMethodCall
因为它假定 2通用参数 (new Type[] { type, selectorResultType }
) 而谓词方法仅使用 1 个通用参数。
这是您实现目标的方法。我尝试以某种方式进行制作,以便您可以遵循表达式构建的每个步骤。
public static IQueryable<TEntity> Where<TEntity>(this IQueryable<TEntity> source, string collectionName, string fieldName, string comparisonOperator, string searchVal) where TEntity : class
{
var entity = Expression.Parameter(source.ElementType, "e");
var collection = Expression.PropertyOrField(entity, collectionName);
var elementType = collection.Type.GetInterfaces()
.Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.GetGenericArguments()[0];
var element = Expression.Parameter(elementType, "i");
var elementMember = Expression.PropertyOrField(element, fieldName);
var elementPredicate = Expression.Lambda(
GenerateComparison(elementMember, comparisonOperator, searchVal),
element);
var callAny = Expression.Call(
typeof(Enumerable), "Any", new[] { elementType },
collection, elementPredicate);
var predicate = Expression.Lambda(callAny, entity);
var callWhere = Expression.Call(
typeof(Queryable), "Where", new[] { entity.Type },
source.Expression, Expression.Quote(predicate));
return source.Provider.CreateQuery<TEntity>(callWhere);
}
private static Expression GenerateComparison(Expression left, string comparisonOperator, string searchVal)
{
var right = Expression.Constant(searchVal);
switch (comparisonOperator)
{
case "==":
case "Equals":
return Expression.Equal(left, right);
case "!=":
return Expression.NotEqual(left, right);
}
return Expression.Call(left, comparisonOperator, Type.EmptyTypes, right);
}
关于c# - LINQ to Entities 基于字符串的动态 Where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132453/