public static EagerlyLoadQueryResult <T> IncludeMediaLibraryPickerFields <T>(this IContentQuery <T> query) where T : class, IContent
        {
            var manager = query.ContentManager;
            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(query.List(), manager);

            return(eagerlyLoadQueryResult.IncludeMediaLibraryPickerFields());
        }
        public static EagerlyLoadQueryResult <T> IncludeContainerContentItems <T>(this IContentQuery <T> query, int maximumLevel = 0) where T : class, IContent
        {
            var manager = query.ContentManager;
            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(query.List(), manager);

            return(eagerlyLoadQueryResult.IncludeContainerContentItems(maximumLevel));
        }
        public static EagerlyLoadQueryResult <T> IncludeTaxonomyFields <T>(this IContentQuery <T> query, bool loadTermsContainter) where T : class, IContent
        {
            var manager = query.ContentManager;

            query = query.Join <TermsPartRecord>().WithQueryHints(new QueryHints().ExpandRecords("TermsPartRecord.Terms"));

            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(query.List(), manager);

            return(eagerlyLoadQueryResult.IncludeTaxonomyFields(loadTermsContainter));
        }
        public static EagerlyLoadQueryResult <T> IncludeContainerContentItems <T>(this EagerlyLoadQueryResult <T> eagerlyLoadQueryResult, int maximumLevel = 0) where T : class, IContent
        {
            var containerIds  = new HashSet <int>();
            var objectsToLoad = eagerlyLoadQueryResult.Result.ToList();

            foreach (var part in objectsToLoad)
            {
                var commonPart = part.As <CommonPart>();
                if (commonPart != null && commonPart.Record.Container != null && !containerIds.Contains(commonPart.Record.Container.Id))
                {
                    containerIds.Add(commonPart.Record.Container.Id);
                }
            }
            var containersDictionary = eagerlyLoadQueryResult.ContentManager.GetTooMany <IContent>(containerIds, VersionOptions.Latest, QueryHints.Empty)
                                       .ToDictionary(c => c.ContentItem.Id);

            foreach (var resultPart in objectsToLoad)
            {
                IContent container  = null;
                var      commonPart = resultPart.As <CommonPart>();
                if (commonPart == null)
                {
                    continue;
                }
                if (commonPart.Record.Container == null)
                {
                    commonPart.Container = null;
                }
                else if (containersDictionary.TryGetValue(commonPart.Record.Container.Id, out container))
                {
                    commonPart.Container = container;
                }
            }
            if (maximumLevel > 0 && containersDictionary.Any())
            {
                containersDictionary.Values.ToList().LoadContainerContentItems(eagerlyLoadQueryResult.ContentManager, maximumLevel - 1);
            }
            return(eagerlyLoadQueryResult);
        }
        public static EagerlyLoadQueryResult <T> IncludeMediaLibraryPickerFields <T>(this EagerlyLoadQueryResult <T> eagerlyLoadQueryResult) where T : class, IContent
        {
            var containerIds = new HashSet <int>();

            foreach (var part in eagerlyLoadQueryResult.Result)
            {
                var mediaLibraryPickerFields = part.ContentItem.Parts.SelectMany(p => p.Fields.Where(f => f is MediaLibraryPickerField).Cast <MediaLibraryPickerField>());
                var ids = mediaLibraryPickerFields.SelectMany(f => f.Ids);
                foreach (var id in ids)
                {
                    if (!containerIds.Contains(id))
                    {
                        containerIds.Add(id);
                    }
                }
            }
            Dictionary <int, MediaPart> containersDictionary = eagerlyLoadQueryResult.ContentManager.GetTooMany <MediaPart>(containerIds, VersionOptions.Published, QueryHints.Empty).ToDictionary(c => c.ContentItem.Id);

            foreach (var resultPart in eagerlyLoadQueryResult.Result)
            {
                var mediaLibraryPickerFields = resultPart.ContentItem.Parts.SelectMany(p => p.Fields.Where(f => f is MediaLibraryPickerField).Cast <MediaLibraryPickerField>());
                foreach (var mediaLibraryPickerField in mediaLibraryPickerFields)
                {
                    var preloadedMedias = new List <MediaPart>();
                    foreach (var mediaId in mediaLibraryPickerField.Ids)
                    {
                        MediaPart preloadedMedia = null;
                        if (containersDictionary.TryGetValue(mediaId, out preloadedMedia))
                        {
                            preloadedMedias.Add(preloadedMedia);
                        }
                    }
                    mediaLibraryPickerField.MediaParts = preloadedMedias;
                }
            }
            return(eagerlyLoadQueryResult);
        }
        public static EagerlyLoadQueryResult <T> LoadMediaLibraryPickerFields <T>(this IList <T> items, IContentManager contentManager, int maximumLevel = 0) where T : class, IContent
        {
            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(items, contentManager);

            return(eagerlyLoadQueryResult.IncludeMediaLibraryPickerFields());
        }
        public static EagerlyLoadQueryResult <T> LoadContainerContentItems <T>(this IList <T> items, IContentManager contentManager, int maximumLevel = 0) where T : class, IContent
        {
            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(items, contentManager);

            return(eagerlyLoadQueryResult.IncludeContainerContentItems(maximumLevel));
        }
        public static EagerlyLoadQueryResult <T> IncludeTaxonomyFields <T>(this EagerlyLoadQueryResult <T> eagerlyLoadQueryResult, bool loadTermsContainter) where T : class, IContent
        {
            var contentManager = eagerlyLoadQueryResult.ContentManager as DefaultContentManager;
            var session        = contentManager.TransactionManager.GetSession();

            Dictionary <int, Dictionary <int, string> > termsTermRecordIdsDictionary = new Dictionary <int, Dictionary <int, string> >();
            var             termsIds    = new HashSet <int>();
            List <Object[]> queryResult = new List <Object[]>();
            int             pageSize    = 2000;
            var             itemsCount  = eagerlyLoadQueryResult.Result.Count();
            var             pagesCount  = (itemsCount + pageSize - 1) / pageSize;


            for (var page = 0; page < pagesCount; page++)
            {
                var objectsToLoad = eagerlyLoadQueryResult.Result.Select(c => c.As <TermsPart>()).Where(t => t != null).ToList();
                if (!objectsToLoad.Any())
                {
                    continue;
                }

                StringBuilder sb = new StringBuilder();

                sb.Append("SELECT tc.TermsPartRecord.Id,tc.TermRecord.id,tc.Field FROM Orchard.Taxonomies.Models.TermContentItem as tc ");
                sb.Append("JOIN tc.TermRecord as tp WHERE ");
                var count = 0;
                foreach (var part in objectsToLoad)
                {
                    sb.Append("tc.TermsPartRecord.id = " + part.Id.ToString());
                    count++;
                    if (count < objectsToLoad.Count)
                    {
                        sb.Append(" OR ");
                    }
                }

                var result = session.CreateQuery(sb.ToString());
                queryResult = result.List <object[]>().ToList();
                foreach (var keyValue in queryResult)
                {
                    var termRecordId = (int)keyValue[1];
                    if (!termsIds.Contains(termRecordId))
                    {
                        termsIds.Add(termRecordId);
                    }

                    if (termsTermRecordIdsDictionary.ContainsKey((int)keyValue[0]))
                    {
                        termsTermRecordIdsDictionary[(int)keyValue[0]].Add(termRecordId, (string)keyValue[2]);
                    }
                    else
                    {
                        Dictionary <int, string> TermsRecordFieldDictionary = new Dictionary <int, string>();
                        TermsRecordFieldDictionary.Add(termRecordId, (string)keyValue[2]);
                        termsTermRecordIdsDictionary.Add((int)keyValue[0], TermsRecordFieldDictionary);
                    }
                }

                var termsDictionary = eagerlyLoadQueryResult.ContentManager.GetTooMany <TermPart>(termsIds, VersionOptions.Published
                                                                                                  , new QueryHints().ExpandRecords("ContentTypeRecord", "CommonPartRecord", "TermsPartRecord"))
                                      .ToDictionary(c => c.ContentItem.Id);

                foreach (var resultPart in objectsToLoad)
                {
                    var fields         = resultPart.ContentItem.Parts.SelectMany(p => p.Fields.OfType <TaxonomyField>());
                    var preloadedTerms = new List <TermContentItemPart>();
                    foreach (var field in fields)
                    {
                        var      preloadedFieldParts = new List <TermPart>();
                        TermPart preloadedPart       = null;
                        Dictionary <int, string> TermsRecordFieldDictionary;
                        if (termsTermRecordIdsDictionary.TryGetValue(resultPart.Id, out TermsRecordFieldDictionary))
                        {
                            foreach (var fieldWithTerm in TermsRecordFieldDictionary)
                            {
                                if (fieldWithTerm.Value == field.Name && termsDictionary.TryGetValue(fieldWithTerm.Key, out preloadedPart))
                                {
                                    preloadedFieldParts.Add(preloadedPart);
                                    preloadedTerms.Add(new TermContentItemPart {
                                        Field    = field.Name,
                                        TermPart = preloadedPart
                                    });
                                }
                            }
                            field.Terms = preloadedFieldParts;
                        }
                        resultPart.TermParts = preloadedTerms;
                    }
                }
                if (loadTermsContainter && termsDictionary.Any())
                {
                    var pendingResults = new EagerlyLoadQueryResult <IContent>(termsDictionary.Values, eagerlyLoadQueryResult.ContentManager);
                    pendingResults.IncludeContainerContentItems(1);
                }
            }

            return(eagerlyLoadQueryResult);
        }
        public static EagerlyLoadQueryResult <T> LoadTaxonomyFields <T>(this IList <T> items, IContentManager contentManager, bool loadTermsContainter) where T : class, IContent
        {
            var eagerlyLoadQueryResult = new EagerlyLoadQueryResult <T>(items, contentManager);

            return(eagerlyLoadQueryResult.IncludeTaxonomyFields(loadTermsContainter));
        }