123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace wispro.sp.share
- {
- [Serializable]
- public class ExpressTree {
- private ConditionTreeNode root ;
-
- public void AddCondition(LogicEnum logic, FieldCondition fieldCondition)
- {
- FieldConditionNode rightNode = new FieldConditionNode() { FieldCondition = fieldCondition };
- if (root == null)
- {
- Console.WriteLine("根节点为空时添加!");
- root = rightNode;
- }
- else
- {
- Console.WriteLine("根节点为空时添加!");
- OperateNode root1 = new OperateNode() { Operator = logic };
- Console.WriteLine("\t设定右节点!");
- root1.Right = rightNode;
- Console.WriteLine("\t设定左节点!");
- root1.Left = root;
- Console.WriteLine("\t设定根节点为操作节点!");
- root = root1;
- Console.WriteLine((root != null)?"\t根节点不为空": "\t根节点为空");
- }
- //Console.WriteLine($"表达式: {ToExpressString("s")}");
- }
- public string ToExpressString(string prefix)
- {
- if(root == null)
- {
- return null;
- }
- else
- {
- if(root is FieldConditionNode)
- {
- return ((FieldConditionNode)root).FieldCondition.ToExpressString(prefix);
- }
- else
- {
- OperateNode operate = (OperateNode)root;
- if(root.Right != null && root.Left != null)
- {
- ExpressTree leftTree = new ExpressTree() { root = root.Left };
- ExpressTree rightTree = new ExpressTree() { root = root.Right };
- switch (operate.Operator)
- {
- case LogicEnum.And:
- return $"({leftTree.ToExpressString(prefix)}) && ({rightTree.ToExpressString(prefix)})";
- break;
- case LogicEnum.Or:
- return $"({leftTree.ToExpressString(prefix)}) || ({rightTree.ToExpressString(prefix)})";
- break;
- default:
- return "";
- }
- }
- else
- {
- throw (new ApplicationException("无效的表达是树!"));
- }
- }
- }
- }
- }
-
- [Serializable]
- public class ConditionTreeNode
- {
- public ConditionTreeNode Right { get; set; }
- public ConditionTreeNode Left { get; set; }
-
- }
- [Serializable]
- public class OperateNode : ConditionTreeNode
- {
- public LogicEnum Operator { get; set; }
- }
- [Serializable]
- public class FieldConditionNode : ConditionTreeNode
- {
- public FieldCondition FieldCondition { get; set; }
- }
- [Serializable]
- public class OrderField
- {
- /// <summary>
- /// 排序栏位
- /// </summary>
- public string FieldName { get; set; }
- /// <summary>
- /// 排序 0:顺序;1:倒序
- /// </summary>
- public int Sort { get; set; }
- }
- [Serializable]
- public class FieldCondition
- {
- /// <summary>
- /// 字段名称
- /// </summary>
- public string FieldName { get; set; }
- /// <summary>
- /// 值
- /// </summary>
- public string Value { get; set; }
- /// <summary>
- /// 值类型
- /// </summary>
- public string ValueType { get; set; }
- /// <summary>
- ///
- /// </summary>
- public OperatorEnum Operator { get; set; }
- public LogicEnum LogicOperate { get; set; }
- public string ToExpressString(string prefix)
- {
- bool isString = (ValueType == typeof(string).ToString());
- switch (Operator) {
- case OperatorEnum.Contains:
- if (isString)
- {
- return $"{prefix}.{FieldName}.Contains(\"{Value}\")";
- }
- else
- {
- throw (new ApplicationException("Contains 操作只对字符字段有效!"));
- }
- break;
- case OperatorEnum.NotContains:
- if (isString)
- {
- return $"!{prefix}.{FieldName}.Contains(\"{Value}\")";
- }
- else
- {
- throw (new ApplicationException("Contains 操作只对字符字段有效!"));
- }
- break;
- case OperatorEnum.Equal:
- if (isString)
- {
- return $"{prefix}.{FieldName} == (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} == ({Value})";
- }
- break;
- case OperatorEnum.NotEqual:
- if (isString)
- {
- return $"{prefix}.{FieldName} != (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} != ({Value})";
- }
- break;
- case OperatorEnum.Greater:
- if (isString)
- {
- return $"{prefix}.{FieldName} > (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} > ({Value})";
- }
- break;
- case OperatorEnum.GreaterEqual:
- if (isString)
- {
- return $"{prefix}.{FieldName} >= (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} >= ({Value})";
- }
- break;
- case OperatorEnum.Less :
- if (isString)
- {
- return $"{prefix}.{FieldName} < (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} < ({Value})";
- }
- break;
- case OperatorEnum.LessEqual:
- if (isString)
- {
- return $"{prefix}.{FieldName} <= (\"{Value}\")";
- }
- else
- {
- return $"{prefix}.{FieldName} <= ({Value})";
- }
- break;
-
- case OperatorEnum.StartsWith:
- if (isString)
- {
- return $"{prefix}.{FieldName}.StartsWith(\"{Value}\")";
- }
- else
- {
- throw (new ApplicationException("StartsWith 操作只对字符字段有效!"));
- }
- break;
- case OperatorEnum.EndWith:
- if (isString)
- {
- return $"{prefix}.{FieldName}.EndWith(\"{Value}\")";
- }
- else
- {
- throw (new ApplicationException("EndWith 操作只对字符字段有效!"));
- }
- break;
- //case OperatorEnum.In:
- // break;
- //case OperatorEnum.Between:
- // break;
- default:
- throw (new ApplicationException("还未实现的操作符号!"));
-
- }
- }
- }
- [Serializable]
- public enum OperatorEnum
- {
- Contains,
- Equal,
- Greater,
- GreaterEqual,
- Less,
- LessEqual,
- NotEqual,
- In,
- Between,
- StartsWith,
- EndWith,
- NotContains
- }
- [Serializable]
- public enum LogicEnum
- {
- And,
- Or
- }
- public static class EFCoreExt
- {
- public static IQueryable<T> OrderConditions<T>(this IQueryable<T> query, IList<OrderField> orderConditions)
- {
- foreach (var orderinfo in orderConditions)
- {
- var parameter = Expression.Parameter(typeof(T));
- var prop = Expression.PropertyOrField(parameter, orderinfo.FieldName);
- var sortLambda = Expression.Lambda(prop, parameter);
- Expression<Func<IOrderedQueryable<T>>> sortMethod = (() => query.OrderBy<T, object>(k => null));
- if(orderinfo.Sort == 1)
- {
- sortMethod = (() => query.OrderByDescending<T, object>(k => null));
- }
- var methodCallExpression = (sortMethod.Body as MethodCallExpression);
- if (methodCallExpression == null)
- throw new Exception("Oops");
- var method = methodCallExpression.Method.GetGenericMethodDefinition();
- var genericSortMethod = method.MakeGenericMethod(typeof(T), prop.Type);
- query = (IQueryable<T>)genericSortMethod.Invoke(query, new object[] { query, sortLambda });
- }
- return query;
- }
- public static IOrderedQueryable<TEntityType> SortMeDynamically<TEntityType>( this IQueryable<TEntityType> query, string propertyname)
- {
- var param = Expression.Parameter(typeof(TEntityType), "s");
- var prop = Expression.PropertyOrField(param, propertyname);
- var sortLambda = Expression.Lambda(prop, param);
- Expression<Func<IOrderedQueryable<TEntityType>>> sortMethod = (() => query.OrderBy<TEntityType, object>(k => null));
- var methodCallExpression = (sortMethod.Body as MethodCallExpression);
- if (methodCallExpression == null)
- throw new Exception("Oops");
- var method = methodCallExpression.Method.GetGenericMethodDefinition();
- var genericSortMethod = method.MakeGenericMethod(typeof(TEntityType), prop.Type);
- var orderedQuery = (IOrderedQueryable<TEntityType>)genericSortMethod.Invoke(query, new object[] { query, sortLambda });
- return orderedQuery;
- }
- public static IQueryable<T> Pager<T>(this IQueryable<T> query, int pageindex, int pagesize, out int itemCount)
- {
- itemCount = query.Count();
- return query.Skip((pageindex - 1) * pagesize).Take(pagesize);
- }
- }
- }
|