Пример #1
0
        public List <MeetingSearchDomainModel> Handle()
        {
            OrgManUnitOfWork uow = new OrgManUnitOfWork();

            DynamicSearchService searchService = new DynamicSearchService();

            Expression <Func <DataModel.Meeting, bool> > whereExpression = null;

            if (_query.SearchCriterias != null)
            {
                try
                {
                    whereExpression = searchService.GetWhereExpression <DataModel.Meeting>(_query.SearchCriterias);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Exception thrown while Building Search Expression from SearchCriteria");
                }
            }

            DynamicOrderService orderService = new DynamicOrderService();

            SortOrderDomainModel orderDomainModel = new SortOrderDomainModel()
            {
                Direction = Common.DynamicOrderService.DynamicOrderModel.Enums.OrderCriteriaDirectionEnum.OrderByDescending,
                Field     = "StartDate"
            };

            var orderQuery = orderService.GetOrderBy <DataModel.Meeting>(orderDomainModel);

            var meetings = uow.MeetingRepository.Get(_query.MandatorUIDs, whereExpression, orderQuery, null, _query.NumberOfRows);

            return(AutoMapper.Mapper.Map <List <MeetingSearchDomainModel> >(meetings));
        }
        public Func <IQueryable <T>, IOrderedQueryable <T> > GetOrderBy <T>(SortOrderDomainModel sortOrder)
        {
            Type typeQueryable = typeof(IQueryable <T>);
            ParameterExpression argQueryable = Expression.Parameter(typeQueryable, "p");
            var outerExpression = Expression.Lambda(argQueryable, argQueryable);

            string[]            props = sortOrder.Field.Split('.');
            IQueryable <T>      query = new List <T>().AsQueryable <T>();
            Type                type  = typeof(T);
            ParameterExpression arg   = Expression.Parameter(type, "x");

            Expression expr = arg;

            foreach (string prop in props)
            {
                PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            LambdaExpression lambda     = Expression.Lambda(expr, arg);
            string           methodName = sortOrder.Direction == OrderCriteriaDirectionEnum.OrderBy ? "OrderBy" : "OrderByDescending";

            MethodCallExpression resultExp =
                Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(T), type }, outerExpression.Body, Expression.Quote(lambda));
            var finalLambda = Expression.Lambda(resultExp, argQueryable);

            return((Func <IQueryable <T>, IOrderedQueryable <T> >)finalLambda.Compile());
        }