本篇文章为大家展示了Linq中怎么自定义组合查询,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
这个表单将Linq自定义组合条件提交后台,我先将它封装成条件对象的数组。
/// /// 条件 /// public class Condition { /// /// 字段 /// public string Field { get; set; } /// /// 表达式 /// public string Operator { get; set; } /// /// 值 /// public string Value { get; set; } /// /// 关系 /// public string Relation { get; set; } /// /// /// /// /// /// /// /// public static Condition[] BuildConditions(string[] fileds,string[] operators,string[] values,string[] relations) { if (fileds == null || operators == null || values == null || relations == null) { return null; } Condition[] conditions = new Condition[fileds.Length]; try { for (int i = 0; i < conditions.Length; i++) { conditions[i] = new Condition() { Field = fileds[i], Operator = operators[i], Value = values[i], Relation = relations[i] }; } } catch { return null; } return conditions; } }
实际上,编译器是把Linq自定义表达式编译成expression tree的形式,我只需要将条件对象数组转换为expression tree就可以了。
我先将一个条件转化为一个简单的expression。
/// /// /// /// /// /// private static Expression ConditonToExpression(Condition condition,Expression parameter) { Expression expr = null; Type type = typeof(EDM_Resource); PropertyInfo pi = type.GetProperty(condition.Field); Expression left = Expression.Property(parameter, pi); object value = Convert.ChangeType(condition.Value, pi.PropertyType); Expression right = Expression.Constant(value); switch (condition.Operator) { case "=": expr = Expression.Equal(left, right); break; case "<": expr = Expression.LessThan(left, right); break; case "<=": expr = Expression.LessThanOrEqual(left, right); break; case ">": expr = Expression.GreaterThan(left, right); break; case ">=": expr = Expression.GreaterThanOrEqual(left, right); break; } return expr; }
然后组合,变成一个lamda表达式,追加到where上。
/// /// /// /// /// /// /// /// /// public IList FindByGroup(EDM_ResGroup resGroup, Condition[] conditions, int first, int limit, out int count) { using (ShengjingEDM2Entities context = new ShengjingEDM2Entities()) { IQueryable result = DoFindByGroup(resGroup, context); ParameterExpression parameter = Expression.Parameter(typeof(EDM_Resource), "r"); Expression body = null; if (conditions != null && conditions.Length > 0) { body = ConditonToExpression(conditions[0], parameter); for (int i = 1; i < conditions.Length; i++) { Expression right = ConditonToExpression(conditions[i],parameter); body = conditions[i - 1].Relation.ToUpper().Equals("AND") ? Expression.And(body, right) : Expression.Or(body, right); } } if (body != null) { Expression<FUNC<EDM_RESOURCE, < SPAN>bool>> expr = Expression.Lambda<FUNC<EDM_RESOURCE, < SPAN>bool>>(body, parameter); result = result.Where(expr); } result = result.OrderByDescending<EDM_RESOURCE, < SPAN>int>(r => r.ResourceID); count = result.Count(); return result .Skip(first) .Take(limit) .ToList(); } }
原来Linq自定义这么强大,这么爽,比拼where条件的方法优雅多了,开发效率也是提高不少,而且我发现性能也不错,100万级的数据通过索引和分页查询还算可以。
上述内容就是Linq中怎么自定义组合查询,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。