这个是jQuery-QueryBuilder的一个插件 构造查询。
UI地址 http://querybuilder.js.org/index.html
对于的c#代码 转化。
https://github.com/castle-it/dynamic-linq-query-builder
上面一段只是介绍 可以忽略
问题开始 了。
这个是js在前台转化的 。希望改成c# 作者也提提供了c# 但是他用的是表达式树 本人不是特别的熟悉 把他的关键代码贴出来
/// <summary> /// Gets the filtered collection after applying the provided filter rules. /// Returns the string representation for diagnostic purposes. /// </summary> /// <typeparam name="T">The generic type.</typeparam> /// <param name="queryable">The queryable.</param> /// <param name="filterRule">The filter rule.</param> /// <param name="parsedQuery">The parsed query.</param> /// <param name="useIndexedProperty">Whether or not to use indexed property</param> /// <param name="indexedPropertyName">The indexable property to use</param> /// <returns>Filtered IQueryable.</returns> public static IQueryable<T> BuildQuery<T>(this IQueryable<T> queryable, FilterRule filterRule, out string parsedQuery, bool useIndexedProperty = false, string indexedPropertyName = null) { if (filterRule == null) { parsedQuery = ""; return queryable; } var pe = Expression.Parameter(typeof(T), "item"); var expressionTree = BuildExpressionTree(pe, filterRule, useIndexedProperty, indexedPropertyName); if (expressionTree == null) { parsedQuery = ""; return queryable; } parsedQuery = expressionTree.ToString(); var whereCallExpression = Expression.Call( typeof(Queryable), "Where", new[] { queryable.ElementType }, queryable.Expression, Expression.Lambda<Func<T, bool>>(expressionTree, pe)); var filteredResults = queryable.Provider.CreateQuery<T>(whereCallExpression); return filteredResults; }
你可以了解一下Specification模式 ,Specification是基于LINQ表达式的 Linq表达式构建器 。
//下面是一段用法示例
ISpecification<Model> spec = SpecificationBuilder.Create<Model>();
spec.Equals(o => o.ID, id);
spec.LessThan(o => o.IsDel, 0);
_list.where(sepc);