2 Replies Latest reply on Nov 19, 2015 12:54 PM by redabakr

    To search element based on attribute value in c#

      Hi All,


      Please provide the code snippet to search element based on attribute value in pi system explorer using c# code?


      Thanks in Advance..





        • Re: To search element based on attribute value in c#
          Marcos Vainer Loeff

          Hello Shifana,


          One possible option would be:


          • Get all attributes that would be included in the search.
          • Perform a single bulk call to get all the attributes snapshots (or end of the stream).
          • Select all the AFValue objects which satisfy your conditions.
          • Return its elements.


          Here is an example for you that will search for elements that has at least one attribute with 5 as the end of the stream:



                      PISystem piSystem = new PISystems()["MARC-PI2014"];
                      AFDatabase db = piSystem.Databases["ANSDK Test"];
                      AFAttributeList attributeList = AFAttribute.FindElementAttributes(db, null, string.Empty, null, null, AFElementType.Any,string.Empty, null, TypeCode.Double,true,AFSortField.Name,AFSortOrder.Ascending,100);
                      AFValues values = attributeList.GetValue();
                      IEnumerable<AFValue> foundValues = values.Where(m => (double)m.Value == 5);
                      IEnumerable<AFElement> foundElements = foundValues.Distinct().Select(m => (AFElement)m.Attribute.Element);


          If have any questions about the LINQ syntax, please don't hesitate to ask.


          Here I am using the FindElementAttributes method but there are more methods and constructors that might be more suitable according to your use case. If performance is important to you, choosing the right method and constructor is critical.


          Finally, foundElements have all elements that has at least one attribute whose end of the stream is 5 by the time the GetValue() call was made.

          • Re: To search element based on attribute value in c#



            I hope this code could help


            //first connect  
            // define rootElement  
              string searchFilter = "my value";  
            AFElementTemplate template = _afDB.ElementTemplates["MyTemplate"];  
            AFAttributeTemplate attributeTemplate = template.AttributeTemplates["INFORMATION"].AttributeTemplates["IP"];  
            AFAttributeValueQuery query1 = new AFAttributeValueQuery(attributeTemplate, OSIsoft.AF.Search.AFSearchOperator.Equal,searchFilter);    
            AFAttributeValueQuery[] queries = new AFAttributeValueQuery[] { query1 };    
            AFNamedCollectionList<AFElement> Elements =  AFElement.FindElementsByAttribute(rootElement, "*", queries, true, AFSortField.Name,AFSortOrder.Ascending, 100);  


            best regards,

            2 of 2 people found this helpful