Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Sims 119 posts 387 karma points
    Sep 30, 2013 @ 23:48
    Michael Sims
    0

    Improved performance

    I am trying to improve the efficiency of this code so that it runs as quickly as possible. Does anyone have any suggestions on how I can speed this up?

     

    // Grab the "Event" document type
    DocumentType dt = DocumentType.GetByAlias("Event");

    // Grab all published events
    var allevents = Document.getContentOfContentType(dt).Where(e => !e.IsTrashed &&
      !string.IsNullOrEmpty(e.getProperty("startDateTime").Value.ToString()) &&
      !string.IsNullOrEmpty(e.getProperty("finishDateTime").Value.ToString())
    ).ToList();


    // Now check to see if we need to show only those related to a certain page
    if (nodeId != "0")
    {
      DocumentType dtPage = DocumentType.GetByAlias("Page");
      var descendants = Document.getContentOfContentType(dtPage).Where(p => !p.IsTrashed &&
      p.Path != null &&
      p.Path.Split(',').Any(i => i == nodeId) &&
      p.Id != null &&
      p.Id.ToString() != nodeId).Select(p => p.Id.ToString()).ToList<string>();
      descendants.Add(nodeId);

      allevents = allevents.Where(e => descendants.Intersect(e.getProperty("relatedPages").Value.ToString().Split(',')).Count() > 0).ToList();
    }

    // Filter by those in the current date range
    var exactmatches = allevents.Where(e => (DateTime.Parse(e.getProperty("startDateTime").Value.ToString()) >= start && DateTime.Parse(e.getProperty("startDateTime").Value.ToString()) <= end)
                    ||
                    (DateTime.Parse(e.getProperty("finishDateTime").Value.ToString()) >= start && DateTime.Parse(e.getProperty("finishDateTime").Value.ToString()) <= end)
    ).ToList();


  • Rich Green 2246 posts 4008 karma points
    Oct 01, 2013 @ 08:09
    Rich Green
    1

    Hey Michael,

    You might want to read this doc http://our.umbraco.org/wiki/reference/api-cheatsheet/difference-between-node-and-document

    You want to be accessing the cached data not the database data, especially as you're only looking at published events.

    Rich

Please Sign in or register to post replies

Write your reply to:

Draft