About

SubSonic is a .net open source project developed by Rob Conery and a core team of developers including Eric Kemp, Scott Watermasysk, Jon Galloway, Phil Haack, and Gavin Joyce. The current stable release is version 2.0.3. Nightly builds are available in our SVN respository.

Tags

Active Record: Using Collections

Strongly-typed collections allow a developer to iterate over a set of data in an intuitive way, utilizing each object's properties and methods rather than typing in strings or "guessing" at ordinal position as you would with a Reader.

Loading A Collection

There are many ways to load a collection - telling it to load itself, or pasing in a DataReader or DataTable. We've tried to make this as simple as we can for you, and here are some examples (all samples below use the Northwind database):

//Self-loading
Northwind
.ProductCollection products = new Northwind.ProductCollection().Load();

 

//using a Reader

IDataReader rdr = Northwind.Product.FetchAll();

Northwind.ProductCollection products = new Northwind.ProductCollection().LoadAndCloseReader(rdr);

You can even send in parameters to the Loader routine:

//Load only Category 5

Northwind.ProductCollection products = new Northwind.ProductCollection().Where("CategoryID", 5).Load();

The Fluent Interface

SubSonic collection methods use what's known as a "Fluent Interface" - meaning that every method returns the current object. In this way you can chain together your method calls for more readable code:

//Load only Category 5 modified in the next 10 days ?

Northwind.ProductCollection products = new Northwind.ProductCollection()

    .Where("CategoryID", 5)

    .WhereDatesBetween("ModifiedOn",DateTime.Now,DateTime.Now.AddDays(10)).Load();

Cloning, Copying, and Basic Movement of Data

You can move data around using collections, and even pass data from one type to another:

//Load up a collection and add it's data to a second collection

Northwind.ProductCollection coll = new Northwind.ProductCollection().Where("CategoryID", 1).Load();

Northwind.ProductCollection coll2 = new Northwind.ProductCollection();

coll2.AddRange(coll.Clone());

You can do this same thing with Copy():

Northwind.ProductCollection coll = new Northwind.ProductCollection().Where("CategoryID", 1).Load();

Northwind.ProductCollection coll2 = new Northwind.ProductCollection();

coll2.CopyFrom(coll);

Finally, you can move data from one type of collection to another, using the ToDataTable() method. In this example, you can move products from the Northwind database to the Southwind database on another server if you like, and then use the SaveAll() method to save the data down to the DB (Note: SaveAll() works within the scope of a transaction):

//Push data from Northwind to Southwind

Northwind.ProductCollection coll = new Northwind.ProductCollection().Load();

DataTable tbl = coll.ToDataTable();

Southwind.ProductCollection coll2 = new Southwind.ProductCollection().Load(tbl);

coll2.SaveAll();

Sorting

Collections support sorting, and you can do a simple sort passing in a column name and specifying if the sort should be ascending:

Northwind.ProductCollection coll = new Northwind.ProductCollection().Load();

coll.Sort("productName", true);

Subscribe