Loading Partial Elements
- Last UpdatedNov 18, 2025
- 10 minute read
- PI System
- AF SDK 2024 R2
- Developer
This topic contains the following sections:
Typically, a top-level object in the AF SDK is either fully-loaded, or just has the header information for it loaded. Often, however, there is a need to just access a few specific attributes from a collection of elements. These examples use the LoadAttributes methods to load a specific list of attributes into the client. By only loading a partial set of attributes, the amount of information required to be transferred from the server to the client is reduced, resulting in faster performance and a lower memory footprint.
Partial Load Elements by IDs
1// Get the Database 2PISystems myPISystems = new PISystems(); 3PISystem myPISystem = myPISystems.DefaultPISystem; 4if (myPISystem == null) 5 throw new InvalidOperationException("Default PISystem was not found."); 6AFDatabase myDB = myPISystem.Databases[dbName]; 7if (myDB == null) 8 throw new InvalidOperationException("Database was not found."); 9 10// Use attribute value query to find a list of tanks that are 11// In Service (tanks which are out-of-service have their 12// Status attribute set to "OS"). 13int totalCount = 0; 14using (var search = new AFElementSearch(myDB, "FindTanks", @"Template:'TankAdvanced' |Status:<>'OS'")) 15{ 16 search.CacheTimeout = TimeSpan.FromMinutes(10); 17 18 // Because we are only going to use a few of the attributes for each 19 // of the elements found, we can reduce the load time and memory space 20 // by just loading the attributes we need. Note that if we inadvertently 21 // use attributes other than these, we will incur a load penalty. 22 AFElementTemplate tankTemplate = myDB.ElementTemplates["Tank"]; 23 AFNamedCollectionList<AFAttributeTemplate> myAttributes = 24 new AFNamedCollectionList<AFAttributeTemplate>(); 25 myAttributes.Add(tankTemplate.AttributeTemplates["Volume"]); 26 myAttributes.Add(tankTemplate.AttributeTemplates["Level"]); 27 myAttributes.Add(tankTemplate.AttributeTemplates["Level|HighLimit"]); 28 myAttributes.Add(tankTemplate.AttributeTemplates["Level|LowLimit"]); 29 30 // Get today's date and UOMs to be used for converting values. 31 AFTime today = new AFTime("T", CultureInfo.CurrentCulture); 32 UOM bbl = myDB.PISystem.UOMDatabase.UOMs["bbl"]; 33 UOM meter = myDB.PISystem.UOMDatabase.UOMs["m"]; 34 35 const int pageSize = 1000; 36 int startIndex = 0; 37 IList<Guid> myElementIds = new List<Guid>(pageSize); 38 do 39 { 40 // Find the element IDs and load the attributes we will be using. 41 var results = search.FindObjectIds(startIndex, pageSize: pageSize); 42 myElementIds.Clear(); 43 int c = 0; 44 foreach (Guid id in results) 45 { 46 totalCount++; 47 myElementIds.Add(id); 48 c++; 49 if (c >= pageSize) 50 break; 51 } 52 53 if (myElementIds.Count == 0) break; 54 AFNamedCollectionList<AFElement> elements = 55 AFElement.LoadAttributes(myPISystem, myElementIds, myAttributes); 56 57 // Once the elements are loaded, we can process them. 58 foreach (AFElement element in elements) 59 { 60 try 61 { 62 AFAttributeList attributes = new AFAttributeList(); 63 attributes.Add(element.Attributes["Volume"]); 64 attributes.Add(element.Attributes["Level"]); 65 attributes.Add(element.Attributes["Level|HighLimit"]); 66 attributes.Add(element.Attributes["Level|LowLimit"]); 67 AFValues values = attributes.GetValue(today); 68 69 if (values[0].IsGood && values[1].IsGood && 70 values[2].IsGood && values[3].IsGood) 71 { 72 double volume = (double)values[0].Convert(bbl).Value; 73 double level = (double)values[1].Convert(meter).Value; 74 double high = (double)values[2].Convert(meter).Value; 75 double low = (double)values[3].Convert(meter).Value; 76 77 Console.WriteLine("Tank Inventory for '{0}' is {1} bbl. %Full={2}", 78 element.Name, volume, 100 * (high - low) / level); 79 } 80 else 81 { 82 Console.WriteLine("Bad data in Tank '{0}'", element.Name); 83 } 84 } 85 catch (FormatException) 86 { 87 Console.WriteLine("Error in Tank '{0}'", element.Name); 88 } 89 } 90 91 startIndex += myElementIds.Count; // Advance to next page. 92 } while (myElementIds.Count > 0 && myElementIds.Count >= pageSize); 93} 94 95Console.WriteLine("Processed {0} Elements.", totalCount);
1' Get the Database 2Dim myPISystems As New PISystems() 3Dim myPISystem As PISystem = myPISystems.DefaultPISystem 4If myPISystem Is Nothing Then 5 Throw New InvalidOperationException("Default PISystem was not found.") 6End If 7Dim myDB As AFDatabase = myPISystem.Databases(dbName) 8If myDB Is Nothing Then 9 Throw New InvalidOperationException("Database was not found.") 10End If 11 12' Use attribute value query to find a list of tanks that are 13' In Service (tanks which are out-of-service have their 14' Status attribute set to "OS"). 15Dim totalCount As Integer = 0 16Using search As New AFElementSearch(myDB, "FindTanks", "Template:'TankAdvanced' |Status:<>'OS'") 17 18 search.CacheTimeout = TimeSpan.FromMinutes(10) 19 20 ' Because we are only going to use a few of the attributes for each 21 ' of the elements found, we can reduce the load time and memory space 22 ' by just loading the attributes we need. Note that if we inadvertently 23 ' use attributes other than these, we will incur a load penalty. 24 Dim tankTemplate As AFElementTemplate = myDB.ElementTemplates("Tank") 25 Dim myAttributes As New AFNamedCollectionList(Of AFAttributeTemplate)() 26 myAttributes.Add(tankTemplate.AttributeTemplates("Volume")) 27 myAttributes.Add(tankTemplate.AttributeTemplates("Level")) 28 myAttributes.Add(tankTemplate.AttributeTemplates("Level|HighLimit")) 29 myAttributes.Add(tankTemplate.AttributeTemplates("Level|LowLimit")) 30 31 ' Get today's date and UOMs to be used for converting values. 32 Dim today As New AFTime("T", CultureInfo.CurrentCulture) 33 Dim bbl As UOM = myDB.PISystem.UOMDatabase.UOMs("bbl") 34 Dim meter As UOM = myDB.PISystem.UOMDatabase.UOMs("m") 35 36 Const pageSize As Integer = 1000 37 Dim startIndex As Integer = 0 38 Dim myElementIds As IList(Of Guid) = New List(Of Guid)(pageSize) 39 Do 40 ' Find the elements and load the attributes we will be using. 41 Dim results As IEnumerable(Of Guid) = search.FindObjectIds(startIndex, pageSize:=pageSize) 42 myElementIds.Clear() 43 Dim c As Integer = 0 44 For Each id As Guid In results 45 totalCount += 1 46 myElementIds.Add(id) 47 c += 1 48 If c >= pageSize Then 49 Exit For 50 End If 51 Next 52 53 If myElementIds.Count = 0 Then 54 Exit Do 55 End If 56 Dim elements As AFNamedCollectionList(Of AFElement) = 57 AFElement.LoadAttributes(myPISystem, myElementIds, myAttributes) 58 59 ' Once the elements are loaded, we can process them. 60 For Each element As AFElement In elements 61 Try 62 Dim attributes As New AFAttributeList() 63 attributes.Add(element.Attributes("Volume")) 64 attributes.Add(element.Attributes("Level")) 65 attributes.Add(element.Attributes("Level|HighLimit")) 66 attributes.Add(element.Attributes("Level|LowLimit")) 67 Dim values As AFValues = attributes.GetValue(today) 68 69 If values(0).IsGood AndAlso values(1).IsGood AndAlso values(2).IsGood AndAlso values(3).IsGood Then 70 Dim volume As Double = CDbl(values(0).Convert(bbl).Value) 71 Dim level As Double = CDbl(values(1).Convert(meter).Value) 72 Dim high As Double = CDbl(values(2).Convert(meter).Value) 73 Dim low As Double = CDbl(values(3).Convert(meter).Value) 74 75 Console.WriteLine("Tank Inventory for '{0}' is {1} bbl. %Full={2}", element.Name, volume, 100 * (high - low) / level) 76 Else 77 Console.WriteLine("Bad data in Tank '{0}'", element.Name) 78 End If 79 Catch generatedExceptionName As FormatException 80 Console.WriteLine("Error in Tank '{0}'", element.Name) 81 End Try 82 Next 83 84 ' Advance to next page. 85 startIndex += myElementIds.Count 86 Loop While myElementIds.Count > 0 AndAlso myElementIds.Count >= pageSize 87End Using 88 89Console.WriteLine("Processed {0} Elements.", totalCount)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Partial Load Elements from Search
1// Get the Database 2PISystems myPISystems = new PISystems(); 3PISystem myPISystem = myPISystems.DefaultPISystem; 4if (myPISystem == null) 5 throw new InvalidOperationException("Default PISystem was not found."); 6AFDatabase myDB = myPISystem.Databases[dbName]; 7if (myDB == null) 8 throw new InvalidOperationException("Database was not found."); 9 10// Use attribute value query to find a list of tanks that are 11// In Service (tanks which are out-of-service have their 12// Status attribute set to "OS"). 13int totalCount = 0; 14using (var search = new AFElementSearch(myDB, "FindTanks", @"Template:'TankAdvanced' |Status:<>'OS'")) 15{ 16 search.CacheTimeout = TimeSpan.FromMinutes(10); 17 18 // Because we are only going to use a few of the attributes for each 19 // of the elements found, we can reduce the load time and memory space 20 // by just loading the attributes we need. Note that if we inadvertently 21 // use attributes other than these, we will incur a load penalty. 22 AFElementTemplate tankTemplate = myDB.ElementTemplates["Tank"]; 23 AFNamedCollectionList<AFAttributeTemplate> myAttributes = 24 new AFNamedCollectionList<AFAttributeTemplate>(); 25 myAttributes.Add(tankTemplate.AttributeTemplates["Volume"]); 26 myAttributes.Add(tankTemplate.AttributeTemplates["Level"]); 27 myAttributes.Add(tankTemplate.AttributeTemplates["Level|HighLimit"]); 28 myAttributes.Add(tankTemplate.AttributeTemplates["Level|LowLimit"]); 29 30 // Get today's date and UOMs to be used for converting values. 31 AFTime today = new AFTime("T", CultureInfo.CurrentCulture); 32 UOM bbl = myDB.PISystem.UOMDatabase.UOMs["bbl"]; 33 UOM meter = myDB.PISystem.UOMDatabase.UOMs["m"]; 34 35 const int pageSize = 1000; 36 int startIndex = 0; 37 IList<AFElement> myElements = new List<AFElement>(pageSize); 38 do 39 { 40 // Find the elements and load the attributes we will be using. 41 var results = search.FindObjects(startIndex, fullLoad: false, pageSize: pageSize); 42 myElements.Clear(); 43 int c = 0; 44 foreach (AFElement item in results) 45 { 46 totalCount++; 47 myElements.Add(item); 48 c++; 49 if (c >= pageSize) 50 break; 51 } 52 53 if (myElements.Count == 0) break; 54 AFElement.LoadAttributes(myElements, myAttributes); 55 56 // Once the elements are loaded, we can process them. 57 foreach (AFElement element in myElements) 58 { 59 try 60 { 61 AFAttributeList attributes = new AFAttributeList(); 62 attributes.Add(element.Attributes["Volume"]); 63 attributes.Add(element.Attributes["Level"]); 64 attributes.Add(element.Attributes["Level|HighLimit"]); 65 attributes.Add(element.Attributes["Level|LowLimit"]); 66 AFValues values = attributes.GetValue(today); 67 68 if (values[0].IsGood && values[1].IsGood && 69 values[2].IsGood && values[3].IsGood) 70 { 71 double volume = (double)values[0].Convert(bbl).Value; 72 double level = (double)values[1].Convert(meter).Value; 73 double high = (double)values[2].Convert(meter).Value; 74 double low = (double)values[3].Convert(meter).Value; 75 76 Console.WriteLine("Tank Inventory for '{0}' is {1} bbl. %Full={2}", 77 element.Name, volume, 100 * (high - low) / level); 78 } 79 else 80 { 81 Console.WriteLine("Bad data in Tank '{0}'", element.Name); 82 } 83 } 84 catch (FormatException) 85 { 86 Console.WriteLine("Error in Tank '{0}'", element.Name); 87 } 88 } 89 90 startIndex += myElements.Count; // Advance to next page. 91 } while (myElements.Count > 0 && myElements.Count >= pageSize); 92} 93 94Console.WriteLine("Processed {0} Elements.", totalCount);
1' Get the Database 2Dim myPISystems As New PISystems() 3Dim myPISystem As PISystem = myPISystems.DefaultPISystem 4If myPISystem Is Nothing Then 5 Throw New InvalidOperationException("Default PISystem was not found.") 6End If 7Dim myDB As AFDatabase = myPISystem.Databases(dbName) 8If myDB Is Nothing Then 9 Throw New InvalidOperationException("Database was not found.") 10End If 11 12' Use attribute value query to find a list of tanks that are 13' In Service (tanks which are out-of-service have their 14' Status attribute set to "OS"). 15Dim totalCount As Integer = 0 16Using search As New AFElementSearch(myDB, "FindTanks", "Template:'TankAdvanced' |Status:<>'OS'") 17 18 search.CacheTimeout = TimeSpan.FromMinutes(10) 19 20 ' Because we are only going to use a few of the attributes for each 21 ' of the elements found, we can reduce the load time and memory space 22 ' by just loading the attributes we need. Note that if we inadvertently 23 ' use attributes other than these, we will incur a load penalty. 24 Dim tankTemplate As AFElementTemplate = myDB.ElementTemplates("Tank") 25 Dim myAttributes As New AFNamedCollectionList(Of AFAttributeTemplate)() 26 myAttributes.Add(tankTemplate.AttributeTemplates("Volume")) 27 myAttributes.Add(tankTemplate.AttributeTemplates("Level")) 28 myAttributes.Add(tankTemplate.AttributeTemplates("Level|HighLimit")) 29 myAttributes.Add(tankTemplate.AttributeTemplates("Level|LowLimit")) 30 31 ' Get today's date and UOMs to be used for converting values. 32 Dim today As New AFTime("T", CultureInfo.CurrentCulture) 33 Dim bbl As UOM = myDB.PISystem.UOMDatabase.UOMs("bbl") 34 Dim meter As UOM = myDB.PISystem.UOMDatabase.UOMs("m") 35 36 Const pageSize As Integer = 1000 37 Dim startIndex As Integer = 0 38 Dim myElements As IList(Of AFElement) = New List(Of AFElement)(pageSize) 39 Do 40 ' Find the elements and load the attributes we will be using. 41 Dim results As IEnumerable(Of AFElement) = search.FindObjects(startIndex, fullLoad:=False, pageSize:=pageSize) 42 myElements.Clear() 43 Dim c As Integer = 0 44 For Each item As AFElement In results 45 totalCount += 1 46 myElements.Add(item) 47 c += 1 48 If c >= pageSize Then 49 Exit For 50 End If 51 Next 52 53 If myElements.Count = 0 Then 54 Exit Do 55 End If 56 AFElement.LoadAttributes(myElements, myAttributes) 57 58 ' Once the elements are loaded, we can process them. 59 For Each element As AFElement In myElements 60 Try 61 Dim attributes As New AFAttributeList() 62 attributes.Add(element.Attributes("Volume")) 63 attributes.Add(element.Attributes("Level")) 64 attributes.Add(element.Attributes("Level|HighLimit")) 65 attributes.Add(element.Attributes("Level|LowLimit")) 66 Dim values As AFValues = attributes.GetValue(today) 67 68 If values(0).IsGood AndAlso values(1).IsGood AndAlso values(2).IsGood AndAlso values(3).IsGood Then 69 Dim volume As Double = CDbl(values(0).Convert(bbl).Value) 70 Dim level As Double = CDbl(values(1).Convert(meter).Value) 71 Dim high As Double = CDbl(values(2).Convert(meter).Value) 72 Dim low As Double = CDbl(values(3).Convert(meter).Value) 73 74 Console.WriteLine("Tank Inventory for '{0}' is {1} bbl. %Full={2}", element.Name, volume, 100 * (high - low) / level) 75 Else 76 Console.WriteLine("Bad data in Tank '{0}'", element.Name) 77 End If 78 Catch generatedExceptionName As FormatException 79 Console.WriteLine("Error in Tank '{0}'", element.Name) 80 End Try 81 Next 82 83 ' Advance to next page. 84 startIndex += myElements.Count 85 Loop While myElements.Count > 0 AndAlso myElements.Count >= pageSize 86End Using 87 88Console.WriteLine("Processed {0} Elements.", totalCount)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.