Beispiel #1
0
        private IQueryable <TEntity> FilterExpression <TEntity>(IQueryable <TEntity> query, IFilterDescriptor filter)
            where TEntity : class
        {
            try {
                Expression <Func <TEntity, bool> > filterExpression = null;

                if (filter is CompositeFilterDescriptor)
                {
                    foreach (IFilterDescriptor childFilter in ((CompositeFilterDescriptor)filter).FilterDescriptors)
                    {
                        query = FilterExpression <TEntity>(query, childFilter);
                    }
                }
                else
                {
                    FilterDescriptor    filterDescriptor = (FilterDescriptor)filter;
                    ParameterExpression paramExp         = Expression.Parameter(typeof(TEntity));

                    Expression exp = filterDescriptor.CreateFilterExpression(paramExp);

                    //MemberExpression fieldExp = Expression.PropertyOrField(paramExp, filterDescriptor.Member);
                    //ConstantExpression valueExp = Expression.Constant(filterDescriptor.Value, filterDescriptor.Value.GetType());

                    //Expression exp = null;

                    //if (fieldExp.Type == typeof(string))
                    //{
                    //    MethodInfo method = typeof(string).GetMethod(filterDescriptor.Operator.ToString(), new[] { typeof(string) });
                    //    exp = Expression.Call(fieldExp, method, valueExp);
                    //}
                    //else if (filterDescriptor.MemberType == typeof(int)
                    //    || filterDescriptor.MemberType == typeof(long))
                    //{
                    //    exp = Expression.LessThanOrEqual(fieldExp, valueExp);
                    //}
                    //else
                    //{
                    //    exp = Expression.Equal(fieldExp, valueExp);
                    //}

                    filterExpression = Expression.Lambda <Func <TEntity, bool> >(exp, paramExp);
                    query            = query.Where(filterExpression);
                }
                return(query);
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }