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);
// 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();
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();
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
is working on a reply...