Working with the CollectionContext class
Working with CollectionContext<T> object
When returning a collection of items in a Sitefinity web service, you should always wrap that collection inside of a CollectionContex<T> object, which provides service client with some additional information about the collection.
The following diagram illustrates CollectionContext<T> class:

Declaring the CollectionContext type
CollectionContext is a generic type, so when declaring it we need to specify the generic argument which is the type of the items this collection holds.
For example, if we are returning a collection of ResourceEntry objects, we would declare CollectionContext object as follows: CollectionContext<ResourceEntry> on the other hand, if our collection is made of Product object, we would declare CollectionContext object like this:
CollectionContext<Product>
Members of the CollectionContext type
CollectionContext class has three public properties:
- IEnumerable<T> Items - Generic IEnumerable of the items of the collection. When you retrieve the items that you wish to return from the web service, you should assign them to this property and return CollectionContext object then.
- int TotalCount - The total count of items that could have been retrieved if skip and take arguments were not specified. This property is generally used for the paging purposes on the client side, where the total count of all possible items must be known, even though the client may receive only 10 or 20 items (depending on the value of "take" parameter)
- bool IsGeneric - Generally speaking, when you receive a collection of items, they will be actual items with properties in JSON. So for example, we may receive a collection of ResourceEntry objects on the client, and to display a value we would call "alert(entry.Value);" property. However, occasionally you will be receiving collection of primitive types (such as string or integer). When receiving such items, the IsGeneric property will be set to true. An example of this is a service which returns a collection of all ResourceClasses (which is basically a collection of strings). On the client we can then check if IsGeneric property is set to true and if so display the class like follows "alert(classId);". The IsGeneric property is set automatically by the CollectionContext class, depending on the generic argument.
Usage example: returning a collection ResourceEntry objects
private CollectionContext<ResourceEntry> GetResourcesInternal(string cultureName, string classId, string provider, string sort, int skip, int take, string filter)
{
var manager = Res.GetManager(provider);
CultureInfo cultureInfo = GetCultureInfo(cultureName);
var query = from resoruce in manager.GetResources(cultureInfo)
where resoruce.ClassId == classId
select resoruce;
// extender takes care of it.
if (!string.IsNullOrEmpty(sort))
query = query.OrderBy(sort);
// this is where the quer the query is executed.
int totalCount = query.Count();
// wont fire the process again, when marked as IEnumerable.
var items = query.AsEnumerable().Skip(skip).Take(take);
var collectionContext = new CollectionContext<ResourceEntry>(items)
{
TotalCount = totalCount
};
return collectionContext;
}