Geeks With Blogs
Scott Kuhl Warning: I may have no idea what I am talking about!


The easiest way to return a list of objects is the FetchAll static method.  It does just what the name says, returning a list of every object of that type in the database in IDataReader format, making it easily bindable to data controls like the GridView.

GridView1.DataSource = Product.FetchAll();

You can also pass the FetchAll method a SubSonic OrderBy.

GridView1.DataSource = Product.FetchAll(SubSonic.OrderBy.Desc(Product.Columns.ProductName));
GridView1.DataSource = Product.FetchAll(SubSonic.OrderBy.Asc(Product.Columns.ProductName));


If you want to list a subset of data rather than the entire list you can use FetchByParameter instead.  At a minimum you only need to supply a column name and value to match.

GridView1.DataSource = Product.FetchByParameter(Product.Columns.SupplierID, 1);

Just like the FetchAll, you can apply an OrderBy.

GridView1.DataSource = Product.FetchByParameter(Product.Columns.SupplierID, 1, SubSonic.OrderBy.Desc(Product.Columns.ProductName));

If you would rather find a range of objects, you can use the overloaded FetchByParameter that takes a SubSonic Comparison.  Again you could also choose to append an OrderBy.

GridView1.DataSource = Product.FetchByParameter(Product.Columns.SupplierID, SubSonic.Comparison.GreaterThan, 1);

Note: SubSonic comparison's will be examined further when discussing querying as well as the FetchByQuery option.


The Find static method allows you to retrieve matching records based on an existing object.  For example, if you want to find all Products with SupplierID of 1 and CategoryID of 1 you can create a new Product object with those values and pass it into the Find method.

Product product = new Product();
product.SupplierID = 1;
product.CategoryID = 1;
GridView1.DataSource = Product.Find(product);

The Find method also has the option of passing an OrderBy.


If none of the Fetch or Find methods will work for a particular situation, you can still fall back on SubSonic querying or stored procedures which are discusses later.

Find the entire series of posts here.

Posted on Tuesday, November 28, 2006 4:47 PM SubSonic | Back to top

Comments on this post: Getting Started with SubSonic - Part 9, Classes - Retrieving Multiple Objects

# re: Getting Started with SubSonic - Part 9, Classes - Retrieving Multiple Objects
Requesting Gravatar...
Usefull :)
Left by Mindaugas on Mar 30, 2009 6:45 AM

# Namespace in DotNet
Requesting Gravatar...
Hello All,

I have uploaded article regarding namespace in dotnet. Read It. It's really useful. How namespace works in .NET Framework

For more information ""

Left by NImesh on Jul 27, 2009 1:23 PM

Your comment:
 (will show your gravatar)

Copyright © Scott Kuhl | Powered by: