1 Reply Latest reply on Sep 29, 2017 6:55 PM by Rick Davin

    AFEventFrameSearch - Last Item


      I am converting some code over from some legacy applications to use the new AFSearch classes.

      My use case is: I want to find the latest EventFrame (most recent StartTime) for a given Element and Event Frame Category.


      Legacy code allowed me to perform an Element.GetEventFrames(...) sort descending on StartTime and MaxCount of 1.


      Its not immediately obvious to me if this same functionality is available.

      string query = string.Format("Element:{0} Category:{1}", element.GetPath(database), myCategory);
      var search = new AFEventFrameSearch(database, "my search", AFSearchMode.StartInclusive, new AFTime( "1/1/2000"),new AFTime("*"), query);
      IEnumerable<AFEventFrame> frames = search.FindEventFrames(startIndex: 0, fullLoad: false, pageSize: 1);
        • Re: AFEventFrameSearch - Last Item
          Rick Davin

          Hi Brendan,


          Interesting question.  Before I get to the heart of your problem, let me address a key point.  You really should wrap the element and category in quotes for the query string, just in case there will ever be embedded blanks.  You may not have blanks now, but one day you might, so you may as well future-proof it.  I think it gets ugly with escaped double quotes so I prefer single quotes:


          string query = string.Format("Element:'{0}' Category:'{1}'", element.GetPath(database), myCategory);


          That was a small, simple change but makes your code less fragile.


          On to the heart of your problem ... you want to use the overload that specifies an AFEventFrameSearchMode.  The only one that fits the bill is this overload..  You would use the AFEventFrameSearchMode.BackwardFromStartTime, also known as "StartingBefore".  While new AFTime("*") is correct, you may consider the more direct AFTime.Now.  The resulting line of code would be:


          var search = new AFEventFrameSearch(database, "my search", AFEventFrameSearchMode.BackwardFromStartTime, AFTime.Now, query);  


          Another way to think of this is to turn your problem around.  You are no longer looking for the last item from a starting point of the beginning of this century.  Rather you are looking for the first item going backwards from Now.

          1 of 1 people found this helpful