PetaPoco – Multiple Result Sets

The other day I got enough time to put the ability to fetch multiple result sets into PetaPoco. Its relatively simple to use and works like this.

Given these two classes with corresponding tables:

    public class Table1
    {
        public string Name { get; set; }
    }
    public class Table2
    {
        public string Col1 { get; set; }
        public int Col2 { get; set; }
    }

Both results can be retrieved in one call to the database like so:

    var result = db.FetchMultiple<Table1, Table2>("select name from table1;" +
                                                  "select col1,col2 from table2");
    var table1list = result.Item1;  //List<Table1>
    var table2list = result.Item2;  //List<Table2>

where result here is a Tuple<List<Table1>, List<Table2>>.

Here the semi-colon represents the separation of the two queries. This is the MS SQL server syntax so please check your database specific syntax to use multiple result sets.

If you don’t like returning a Tuple from this query you can provide your own callback to the FetchMultiple method. The callback for the above query would be of type. Func<List<Table1>, List<Table2>, ReturnClass>

The feature is currently only available in my master branch where you can find the download package here https://github.com/schotime/PetaPoco/downloads or directly https://github.com/downloads/schotime/PetaPoco/Schotime-PetaPoco-4.0.3.5.zip.

Hope you find it useful.

One Response to PetaPoco – Multiple Result Sets

  1. Eduardo says:

    I’m not sure if I get the point on this one, but good to know it can be done. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>