Click or drag to resize
OSIsoft, LLC

AFSDKExtensionChunkedByT Method

This extension method breaks up search results into chunks to make it easier to page through and process IEnumerableT collections in chunks.

Namespace:  OSIsoft.AF
Assembly:  OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 2.10.8.440
Syntax
public static IEnumerable<IList<T>> ChunkedBy<T>(
	this IEnumerable<T> enumerable,
	int chunkSize
)

Parameters

enumerable
Type: System.Collections.GenericIEnumerableT
The IEnumerableT collection being broken into chunks of items.
chunkSize
Type: SystemInt32
The size of the list of returned items for each chunk of items from the collection.

Type Parameters

T
The type of items the list being broken into chunks.

Return Value

Type: IEnumerableIListT
Returns a list of items of chunkSize from the enumerable collection. The last chunk of items maybe less than the specified chunk size.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentOutOfRangeException This exception is thrown if the chunkSize parameter is less than or equal to zero.
Remarks
Many times it is more efficient to page through and process a page of search results in chunks instead of the entire collection. For example, the DeleteElements(PISystem, IListGuid) method takes a list of IDs of the elements to be deleted. Instead of passing in the entire list from the search, it is more efficient to break the results into smaller chunks to be deleted.
Examples
// This example demonstrates how to use efficiently delete elements using
//  AFElementSearch and the DeleteElements method.

// Get the Database
PISystems myPISystems = new PISystems();
PISystem myPISystem = myPISystems.DefaultPISystem;
AFDatabase myDB = myPISystem.Databases.DefaultDatabase;

// Search for the elements to be deleted.
using (var search = new AFElementSearch(myDB, "FindElements", @"Template:'MyTemplate'"))
{
    search.CacheTimeout = TimeSpan.FromMinutes(10);

    // Get a list of IDs of elements to be deleted in pages of 100.
    foreach (IList<Guid> pageOfIds in search.FindObjectIds().ChunkedBy(100))
    {
        AFElement.DeleteElements(myPISystem, pageOfIds);
    }
}
Version Information

AFSDK

Supported in: 2.10.5
See Also
Enabling Operational Intelligence