title: Sitefinity: Dynamic Content query optimization with field values
body: <p>I will attempt to be as specific as possible. So we are using <strong>Sitefinity 8.1.5800</strong>, I have a couple dynamic content modules named ReleaseNotes and ReleaseNoteItems. ReleaseNotes has some fields but no reference to ReleaseNoteItems. </p>
  
  <p>Release Note Items has fields and a related data field to ReleaseNotes. </p>
  
  <p>So I can query all ReleaseNoteItems as dynamic content pretty quickly less than a second.</p>
  
  <p>I then use these objects provided by sitefinity and map them to a C# object so I can use strong type. This mapping process is taking almost a minute and using over 600 queries for only 322 items (N+1).</p>
  
  <p><strong>In Short:</strong> I need to get all sitefinity objects and Map them to a usable c# object quicker than I currently am.</p>
  
  <p>The method for fetching the dynamic content items (takes milliseconds):</p>
  
  <hr>
  
  <pre><code>private IList&lt;DynamicContent&gt; GetAllLiveReleaseNoteItemsByReleaseNoteParentId(Guid releaseNoteParentId)
         {
             DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(String.Empty);
             Type releasenoteitemType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ReleaseNoteItems.Releasenoteitem");
             string releaseNoteParentTypeString = "Telerik.Sitefinity.DynamicTypes.Model.ReleaseNotes.Releasenote";
             var provider = dynamicModuleManager.Provider as OpenAccessDynamicModuleProvider;
             int? totalCount = 0;
  
             var cultureName = "en";
             Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
  
  
             Type releasenoteType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ReleaseNotes.Releasenote");
  
             // This is how we get the releasenote items through filtering
  
             DynamicContent myCurrentItem = dynamicModuleManager.GetDataItem(releasenoteType, releaseNoteParentId);
  
             var myMasterParent =
                 dynamicModuleManager.Lifecycle.GetMaster(myCurrentItem) as DynamicContent;
  
             var relatingItems = provider.GetRelatedItems(
                  releaseNoteParentTypeString,
                  "OpenAccessProvider",
                  myMasterParent.Id,
                  string.Empty,
                  releasenoteitemType,
                  ContentLifecycleStatus.Live,
                  string.Empty,
                  string.Empty,
                  null,
                  null,
                  ref totalCount,
                  RelationDirection.Parent).OfType&lt;DynamicContent&gt;();
             IList&lt;DynamicContent&gt; allReleaseNoteItems = relatingItems.ToList();
  
             return allReleaseNoteItems;
         }
  </code></pre>
  
  <hr>
  
  <p>This is the method that takes almost a minute that is mapping sitefinity object to C# object:</p>
  
  <pre><code>public IList&lt;ReleaseNoteItemModel&gt; GetReleaseNoteItemsByReleaseNoteParent(ReleaseNoteModel releaseNoteItemParent)
         {
  
            return  GetAllLiveReleaseNoteItemsByReleaseNoteParentId(releaseNoteItemParent.Id).Select(rn =&gt; new ReleaseNoteItemModel
             {
  
                 Id = rn.Id,
                 Added = rn.GetValue("Added") is bool ? (bool)rn.GetValue("Added") : false,
                 BugId = rn.GetValue&lt;string&gt;("bug_id"),
                 BugStatus = rn.GetValue&lt;Lstring&gt;("bugStatus"),
                 Category = rn.GetValue&lt;Lstring&gt;("category"),
                 Component = rn.GetValue&lt;Lstring&gt;("component"),
                 @Content = rn.GetValue&lt;Lstring&gt;("content"),
                 Criticality = rn.GetValue&lt;Lstring&gt;("criticality"),
                 Customer = rn.GetValue&lt;string&gt;("customer"),
                 Date = rn.GetValue&lt;DateTime?&gt;("date"),
                 Grouped = rn.GetValue&lt;string&gt;("grouped"),
                 Override = rn.GetValue&lt;string&gt;("override"),
                 Patch_Num = rn.GetValue&lt;string&gt;("patch_num"),
                 PublishedDate = rn.PublicationDate,
                 Risk = rn.GetValue&lt;Lstring&gt;("risk"),
                 Title = rn.GetValue&lt;string&gt;("Title"),
                 Summary = rn.GetValue&lt;Lstring&gt;("summary"),
                 Prod_Name = rn.GetValue&lt;Lstring&gt;("prod_name"),
                 ReleaseNoteParent = releaseNoteItemParent,
                 McProductId = GetMcProductId(rn.GetRelatedItems("McProducts").Cast&lt;DynamicContent&gt;()),
  
             }).ToList();
  
         }
  </code></pre>
  
  <hr>
  
  <p>Is there any way to optimize this all into one query or a better way of doing this? Taking almost a minute to map this objects is too long for what we need to do with them. </p>
  
  <p>If there is no way we will have to cache the items or make a SQL query. I would rather not do caching or SQL query if I do not have to. </p>
  
  <p>Thank you in advance for any and all help you can provide, I am new to posting questions on stackoverflow so if you need any additional data please let me know.</p>