c# Lambda操作类封装
程序员文章站
2022-05-12 12:35:02
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using Sy... ......
using system; using system.collections.generic; using system.linq; using system.linq.expressions; using system.reflection; using system.text; using system.threading.tasks; namespace easyframe.common { public static class lambdacommon { #region 表达式工具 /// <summary> /// 相当于&&操作 /// ——author:hellthinker /// </summary> /// <param name="thisfilter">已生成的过滤条件</param> /// <param name="otherfilter">未生成的过滤条件</param> /// <returns>新的过滤</returns> public static expression gotoandalso(this expression thisfilter, expression otherfilter) { return expression.andalso(thisfilter, otherfilter); } /// <summary> /// 相当于||操作 /// ——author:hellthinker /// </summary> /// <param name="thisfilter">已生成的过滤条件</param> /// <param name="otherfilter">未生成的过滤条件</param> /// <returns>新的过滤</returns> public static expression gotoorelse(this expression thisfilter, expression otherfilter) { return expression.orelse(thisfilter, otherfilter); } /// <summary> /// 相当于==操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotoequal(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { return expression.equal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue)); } /// <summary> /// 相当于>=操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotogreaterthanorequal<t>(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //大于或等于 return expression.greaterthanorequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(t))); } /// <summary> /// 相当于小于等于操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotolessthanorequal<t>(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //小于或等于 return expression.lessthanorequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(t))); } /// <summary> /// 相当于!=操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotonotequal(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { return expression.notequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue)); } /// <summary> /// 相当于>=操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotogreaterthanorequal(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //大于或等于 return expression.greaterthanorequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue)); } /// <summary> /// 相当于>操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotogreaterthan<t>(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //大于 return expression.greaterthan(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(t))); } /// <summary> /// 相当于小于操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotolessthan<t>(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //小于 return expression.lessthan(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(t))); } /// <summary> /// 相当于>=操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotogreaterthanorequalbydatetime(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //大于或等于 return expression.greaterthanorequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(datetime?))); } /// <summary> /// 字符串包含 /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotocontains(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { return expression.call(expression.property(thisparameterexpression, propertiename), typeof(string).getmethod("contains"), expression.constant(propertievalue)); } /// <summary> /// 相当于小于或等于操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotolessthanorequalbydatetime(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //小于或等于 return expression.lessthanorequal(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(datetime?))); } /// <summary> /// 相当于>操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotogreaterthanbydatetime(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //大于 return expression.greaterthan(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(datetime?))); } /// <summary> /// 相当于小于操作 /// ——author:hellthinker /// </summary> /// <param name="thisparameterexpression">查询对象</param> /// <param name="propertiename">属性名称</param> /// <param name="propertievalue">属性值</param> /// <returns>新的过滤</returns> public static expression gotolessthanbydatetime(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { //小于 return expression.lessthan(expression.property(thisparameterexpression, propertiename), expression.constant(propertievalue, typeof(datetime?))); } /// <summary> /// 包含操作 相当余 a=> arr.contains(a.id) /// </summary> /// <param name="thisparameterexpression"></param> /// <param name="propertiename"></param> /// <param name="propertievalue"></param> /// <returns></returns> public static expression containsoperations(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { methodinfo method = null; memberexpression member = expression.property(thisparameterexpression, propertiename); var containsmethods = typeof(enumerable).getmethods(bindingflags.static | bindingflags.public).where(m => m.name == "contains"); foreach (var m in containsmethods) { if (m.getparameters().count() == 2) { method = m; break; } } method = method.makegenericmethod(member.type); var exprcontains = expression.call(method, new expression[] { expression.constant(propertievalue), member }); return exprcontains; } /// <summary> /// 包含操作 相当于 a=>a.id.contains(key) /// </summary> /// <param name="thisparameterexpression"></param> /// <param name="propertiename"></param> /// <param name="propertievalue"></param> /// <returns></returns> public static expression contains(this parameterexpression thisparameterexpression, string propertiename, object propertievalue) { var propertyexp = expression.property(thisparameterexpression, propertiename); methodinfo method = typeof(string).getmethod("contains", new[] { typeof(string) }); var somevalue = expression.constant(propertievalue, typeof(string)); var containsmethodexp = expression.call(propertyexp, method, somevalue); return containsmethodexp; } #endregion } }
下一篇: BAT百度,为什么离我们越来越远?