Пример #1
0
        /// <summary>
        /// Applies a specified sort order to an <see cref="IQueryable[]"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the element in the <see cref="IQueryable[]"/>.</typeparam>
        /// <param name="instance">The instance of <see cref="IQueryable[]"/> to be extended.</param>
        /// <param name="property">The name of the property to be the order by key.</param>
        /// <param name="sortOrder">The type of sort order to apply to the <see cref="IQueryable[]"/></param>
        /// <returns>A sorted <see cref="IOrderedQueryable[]"/></returns>
        public static IOrderedQueryable <TSource> ApplyOrder <TSource>(this IQueryable <TSource> instance, string property, QuerySortOrder sortOrder)
        {
            string[]            props = property.Split('.');
            Type                type  = typeof(TSource);
            ParameterExpression arg   = Expression.Parameter(type, "x");
            Expression          expr  = arg;

            foreach (string prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                PropertyInfo pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }

            Type             delegateType = typeof(Func <,>).MakeGenericType(typeof(TSource), type);
            LambdaExpression lambda       = Expression.Lambda(delegateType, expr, arg);

            object result = typeof(Queryable).GetMethods().Single(
                method => method.Name == sortOrder.ToString() &&
                method.IsGenericMethodDefinition &&
                method.GetGenericArguments().Length == 2 &&
                method.GetParameters().Length == 2)
                            .MakeGenericMethod(typeof(TSource), type)
                            .Invoke(null, new object[] { instance, lambda });

            return((IOrderedQueryable <TSource>)result);
        }
Пример #2
0
        public Tridion.ContentDelivery.DynamicContent.Query.Query ToTridionQuery()
        {
            string[] basedOnSchemas              = QuerySchemas;
            DateTime lastPublishedDate           = LastPublishedDate;
            IList <MetaQueryItem> metaQueryItems = MetaQueryValues;
            QueryLogic            metaQueryLogic = MetaQueryLogic;
            int maxmimumComponents = MaximumComponents;

            Query q = null;
            //PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            //format DateTime // 00:00:00.000
            ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(lastPublishedDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), Criteria.GreaterThanOrEqual);
            //publicationAndLastPublishedDateCriteria.AddCriteria(dateLastPublished);

            Criteria basedOnSchemaAndInPublication;

            if (basedOnSchemas.Length > 0)
            {
                Criteria[] schemaCriterias = new Criteria[basedOnSchemas.Length];
                int        i = 0;
                foreach (var schema in basedOnSchemas)
                {
                    TcmUri schemaUri = new TcmUri(schema);
                    schemaCriterias.SetValue(new ItemSchemaCriteria(schemaUri.ItemId), i);
                    i++;
                }
                Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
                basedOnSchemaAndInPublication = CriteriaFactory.And(publicationAndLastPublishedDateCriteria, basedOnSchema);
            }
            else
            {
                basedOnSchemaAndInPublication = publicationAndLastPublishedDateCriteria;
            }

            // Add filtering for meta data
            Criteria schemasAndMetaData;

            if (metaQueryItems.Count > 0)
            {
                Criteria   metaQuery;
                Criteria[] metaCriterias = new Criteria[metaQueryItems.Count];
                int        metaCount     = 0;
                foreach (MetaQueryItem queryItem in metaQueryItems)
                {
                    CustomMetaKeyCriteria   metaField = new CustomMetaKeyCriteria(queryItem.MetaField);
                    CustomMetaValueCriteria metaCriteria;
                    FieldOperator           metaOperator = typeof(Criteria).GetField(queryItem.MetaOperator.ToString()).GetValue(null) as FieldOperator;

                    switch (queryItem.MetaValue.GetType().Name)
                    {
                    case "DateTime":
                        DateTime tempDate = (DateTime)queryItem.MetaValue;
                        metaCriteria = new CustomMetaValueCriteria(metaField, tempDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.SSS", metaOperator);
                        break;

                    case "Float":
                        metaCriteria = new CustomMetaValueCriteria(metaField, (float)queryItem.MetaValue, metaOperator);
                        break;

                    case "String":
                        metaCriteria = new CustomMetaValueCriteria(metaField, queryItem.MetaValue as string, metaOperator);
                        break;

                    default:
                        throw new System.Exception("Unexpected query item data type; " + queryItem.MetaValue.GetType().Name);
                    }

                    metaCriterias.SetValue(metaCriteria, metaCount);
                    metaCount++;
                }

                if (MetaQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    metaQuery = CriteriaFactory.And(metaCriterias);
                }
                else
                {
                    metaQuery = CriteriaFactory.Or(metaCriterias);
                }
                schemasAndMetaData = CriteriaFactory.And(basedOnSchemaAndInPublication, metaQuery);
            }
            else
            {
                schemasAndMetaData = basedOnSchemaAndInPublication;
            }

            Criteria allConditions;

            if (KeywordValues.Count > 0)
            {
                Criteria[] keywordCriterias = new Criteria[KeywordValues.Count];
                int        keywordCount     = 0;
                foreach (KeywordItem keyCriteria in KeywordValues)
                {
                    TaxonomyKeywordCriteria keywordField = new TaxonomyKeywordCriteria(keyCriteria.CategoryUri, keyCriteria.KeywordUri, false);
                    keywordCriterias.SetValue(keywordField, keywordCount);
                    keywordCount++;
                }

                Criteria keyQuery;
                if (KeywordQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    keyQuery = CriteriaFactory.And(keywordCriterias);
                }
                else
                {
                    keyQuery = CriteriaFactory.Or(keywordCriterias);
                }
                allConditions = CriteriaFactory.And(schemasAndMetaData, keyQuery);
            }
            else
            {
                allConditions = schemasAndMetaData;
            }


            q = new Query(allConditions);
            if (maxmimumComponents != 0 && maxmimumComponents != int.MaxValue)
            {
                LimitFilter limitResults = new LimitFilter(maxmimumComponents);
                q.SetResultFilter(limitResults);
            }

            // Sort column should either be a standard or custom metaData field
            SortColumn paramSort;

            if (typeof(SortParameter).GetField(QuerySortField) != null)
            {
                paramSort = typeof(SortParameter).GetField(QuerySortField).GetValue(null) as SortColumn;
            }
            else
            {
                // Why do we need to tell Tridion what data type the field is! Its in the database already!
                /* FIX: Does not work (null pointer exception): var sType = typeof(MetadataType).GetField(SortType.ToString()).GetValue(null) as MetadataType;*/
                paramSort = new CustomMetaKeyColumn(QuerySortField, SortType);
            }
            SortDirection paramSortDirection = typeof(SortParameter).GetField(QuerySortOrder.ToString()).GetValue(null) as SortDirection;
            SortParameter sortParameter      = new SortParameter(paramSort, paramSortDirection);

            q.AddSorting(sortParameter);
            return(q);
        }