コード例 #1
0
        private IQueryable <T> AddCriteria <T>(IQueryable <T> dbSet, QueryCriteriaDto criteria) where T : BaseModel
        {
            var parameter = Expression.Parameter(typeof(T), "x");


            var property = Expression.PropertyOrField(parameter, criteria.Property);

            var left  = property;
            var rigth = Expression.Constant(criteria.Value);

            BinaryExpression whereBody;

            if (criteria.Operator == Operators.EQUAL)
            {
                whereBody = Expression.Equal(left, rigth);
            }
            else if (criteria.Operator == Operators.GRATER_THAN_OR_EQUAL)
            {
                whereBody = Expression.GreaterThanOrEqual(left, rigth);
            }
            else if (criteria.Operator == Operators.LESS_THAN_OR_EQUAL)
            {
                whereBody = Expression.LessThanOrEqual(left, rigth);
            }
            else if (criteria.Operator == Operators.NOT_EQUAL)
            {
                whereBody = Expression.NotEqual(left, rigth);
            }
            else
            {
                whereBody = Expression.Equal(left, rigth);
            }

            var whereLambda = Expression.Lambda <Func <T, bool> >(whereBody, parameter);

            var whereCallExpression = Expression.Call(
                typeof(IQueryable),
                "Where",
                new[] { typeof(T) },
                dbSet.Expression,
                whereLambda
                );

            return(dbSet.Provider.CreateQuery <T>(whereCallExpression));
        }
コード例 #2
0
 private IQueryable <T> AddCriteria <T>(T _, IQueryable <T> dbSet, QueryCriteriaDto criteria) where T : BaseModel
 {
     return(AddCriteria(dbSet, criteria));
 }