At the request of a responder to my original question here, I have been asked to reword the question in an effort to get to the bottom of the actual requirement.
What Strongly-Typed Data-Structure can I use to hold a collection of multiple RecordSets where each RecordSet will contain rows that may be of a different shape to the other RecordSets?
This need is driven by the need to handle the data coming back from a Stored Procedure via a DbDataReader. The Stored Procedure may have multiple RecordSets, with each RecordSet returning different columns and number of columns.
There will be DTO classes to represent each row of the respective data sets. These are known at compile time. What I need is a data structure that can hold the multiple RecordSets, and for each RecordSet hold the DTOs representing rows returned from the RecordSet.
Example Stored procedure:
CREATE PROCEDURE [dbo].[MultipleRecordSetStoredProcedure]
@Id INT
, @Name VARCHAR(20)
, @Active BIT
, @Price DECIMAL(10, 4)
, @UniqueIdentifier UNIQUEIDENTIFIER
, @Count TINYINT
AS
BEGIN
/* First Record Set */
SELECT
@Id AS Id
, @Name AS Name
UNION
SELECT
17 AS Id
, 'Bill' AS Name;
/* Second Record Set */
SELECT
@Name AS Name,
@Active as Active
UNION
SELECT
'Bill' AS Name
, CAST(0 AS BIT) AS Active;
/* Third Record Set */
SELECT
@UniqueIdentifier AS UNIQUEIDENTIFIER
, @Count AS Count
UNION
SELECT
NEWID() AS UNIQUEIDENTIFIER
, CAST(10 AS TINYINT) AS Count;
END
Example calling code:
DbConnection connection = CreateConnection();
CommandBehavior commandBehavior = CommandBehavior.Default;
// Create a command to execute the stored storedProcedure
using (DbCommand command = connection.CreateStoredProcedureCommand(
procedureName,
procedureParameters,
commandTimeout,
transaction))
{
// Populate a DataReder by calling the command
using (DbDataReader reader = command.ExecuteReader(commandBehavior))
{
// Iterate through each result set...
do
{
// Process the result set line by line
while (reader.Read())
{
// Read data into DataStructure
}
} while (reader.NextResult());
}
}
Example DTOs here:
internal class MultipleRecordSetStoredProcedureReturnType1
{
public int Id { get; set; }
public string Name { get; set; }
}
internal class MultipleRecordSetStoredProcedureReturnType2
{
public bool Active { get; set; }
public decimal Decimal { get; set; }
}
internal class MultipleRecordSetStoredProcedureReturnType3
{
public Guid UniqueIdentifier { get; set; }
public Byte Count { get; set; }
}
Ideally I do not want a list of objects or dynamics but a list of my DTOs for the recordset contents. Hopefully this will better clarify my original question.
In this case, I think it's better to keep things simple.
ResultModel
, its better to useSqlDataAdapter
andDataSet
. It make things very simple, very very easier than data reader.The code for ResultModel:
And I suppose these are your types:
And you can fill your ResultModel, using SqlDataAdapter and DataSet:
The key points here are:
ResultModel
SqlDataAdapter
and aDataSet
makes reading multiple result so easy.Cast<DataRow>()
enables us to useLinq
againstDataTable.Rows
Field<T>("field")
enables us to get typed value of fieldI'm not sure it makes sense to return a
List
containing disparate results. I'd probably create a wrapper class to group your dto objects like below.If you wanted to take the db code a step further to make it generic you could do something along these lines.
Not the prettiest or readable code I admit but it should allow you to call any multi record set stored procedure as long as the property names match the column names. One downside with this code is that while the compiler will enforce you to only select
IList
properties in you expressions it is unable to ensure you pick a generic list (which the method itself actually requires).For your example case the method would be called as below
Having read the original question, and this one, it sounds like you will ultimately end up with a List that contains within it other Lists each with their own type. As you iterate through the list you'll have no idea of what type to expect for the inner list, as it's variable. Given this you'll inevitably have to test for the type and then cast it, e.g.
Given that you'll inevitably have to test for the inner type and cast, even if you could achieve your goal I'm not sure it will serve any purpose in the end. The list may as well just be
or perhaps
As per the final example, you could consider defining an Interface for the inner type just to ensure that only objects that inherit from that interface are contained within it. This may be beneficial even if the interface doesn't define any behaviour and is just a simple declaration.