public TListLogic <T> TOrder(string key = null, bool isAsc = true) { while (true) { TListOrder listOrder = null; if (!string.IsNullOrWhiteSpace(key)) { // input sort listOrder = new TListOrder(key, isAsc); } else { // config sort listOrder = TType.GetCustomAttribute <TListOrder>(); if (listOrder == null) { // DefaultKey sort listOrder = new TListOrder(PropertyHelper.DefaultKey, isAsc); } } // check key property PropertyInfo property = TType.GetProperty(listOrder.Key); if (property == null) { break; } ParameterExpression parameter = Expression.Parameter(TType); MemberExpression propertyAccess = Expression.MakeMemberAccess(parameter, property); LambdaExpression lambda = Expression.Lambda(propertyAccess, parameter); // pre_department.Name string order = listOrder.IsAsc ? "OrderBy" : "OrderByDescending"; m_query = Expression.Call( typeof(Queryable), order, new Type[] { TType, property.PropertyType }, m_query, lambda ); m_isOrder = true; break; } return(this); }
public TListLogic <T> TWhere(string key, object value, string operate = op.eq) { while (true) { // check key input if (string.IsNullOrWhiteSpace(key)) { break; } // check key property PropertyInfo property = TType.GetProperty(key); if (property == null) { break; } // convert type if (value.GetType() != property.PropertyType) { THelper.ConvertToType(property.PropertyType, ref value); } ParameterExpression parameter = Expression.Parameter(TType); Expression left = Expression.Property(parameter, property); Expression right = Expression.Constant(value); if (value.GetType() != property.PropertyType) { right = Expression.Convert(right, property.PropertyType); } Expression condition = null; switch (operate) { case op.gt: condition = Expression.GreaterThan(left, right); break; case op.gte: condition = Expression.GreaterThanOrEqual(left, right); break; case op.lt: condition = Expression.LessThan(left, right); break; case op.lte: condition = Expression.LessThanOrEqual(left, right); break; case op.like: Type stringType = typeof(string); condition = Expression.Call( left, stringType.GetMethod("Contains", new Type[] { stringType }), right); break; case op.eq: default: condition = Expression.Equal(left, right); break; } LambdaExpression lambda = Expression.Lambda(condition, parameter); m_query = Expression.Call( typeof(Queryable), "Where", new Type[] { TType }, m_query, lambda ); break; } return(this); }