4 Replies Latest reply on Jan 11, 2017 4:27 PM by gregor

    Find elements by name


      Dear all,


      What is the most efficient way to search for a list of AF elements by name?

      I have an array of names (strings) and I would like to get the list of elements, anywhere in the tree.

      For example, a function like


      IEnumerable<AFElement> FindElementsByName(AFElement searchElement, IEnumerable<string> elementNames)

      Is there an efficient way to perform this search?





        • Re: Find elements by name
          Rhys Kirk

          It depends...you could do an inefficient query by parsing the list of names one time; searching on each one and then caching your own AF Paths to the objects. Then you only do a bulk "AFElement.FindElementsByPath".


          Otherwise, it depends on your list, how big you expect it to be, whether it is user entered etc. If you're looking for multiple Units, for example, then you can use the name filter syntax to do that...


          var elementsFound = AFElement.FindElements(db, null, "Unit [12345]", OSIsoft.AF.AFSearchField.Name, true, OSIsoft.AF.AFSortField.Name, OSIsoft.AF.AFSortOrder.Ascending, 10000);


          "Unit [12345]" would find "Unit 1", "Unit 2", "Unit 3", "Unit 4" and "Unit 5". In a single call, rather than supplying those 5 names in a list.

          Really depends on how your list of names is built.

          • Re: Find elements by name

            you might want to check out the new search function in the AF SDK 2016. Why you should use the new AF SDK Search

            I hope this help!

            • Re: Find elements by name

              Hi Ricky,


              Thank you for the input. The AF search looks very interesting.

              I have tried the following search operation:


                   List<AFSearchToken> tokens = elementNames.Select(name => new AFSearchToken(AFSearchFilter.Name, AFSearchOperator.Equal, name)).ToList();


                   var afElementSearch = new AFElementSearch(afDatabase, "", tokens);

              Unfortunately, it always returns only one element, which there are more elements.

              Is it because I have multiple tokens with AFSearchFilter.Name? I would like to use an OR-statement between the several tokens.