Multiple Results and Multiple Mapping
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I have the following four entities:
public class BasicCellModel
{
public int Id { get; set; }
public string CellName { get; set; }
public DateTime CreateDate { get; set; }
public DateTime LastUpdated { get; set; }
}
public class BasicStationModel
{
public int Id { get; set; }
public string StationName { get; set; }
public DateTime CreateDate { get; set; }
public DateTime LastUpdated { get; set; }
}
public class FullCellModel
{
public BasicCellModel BasicInfo { get; set; }
public BasicVoltageModel CellVoltage { get; set; }
public List<FullRelayModel> Relays { get; set; }
}
public class FullStationModel
{
public BasicStationModel BasicInfo { get; set; }
public List<FullCellModel> Cells { get; set; } = new List<FullCellModel>();
}
I want the result to be a FullStationModel object with a List<FullCellModel> in Cells and all FullCellModel objects having BasicStationModel BasicInfo only.

The SQL query and C# code is as below:
var sql = @"select * from dbo.Stations where Id = @Id
select c.* from dbo.Cells as c
inner join dbo.StationsCellsVoltages as scv
on scv.CellId = c.Id
where scv.StationId = @StationId;";
var fullstation = db.MultipleQuery<FullCellModel, dynamic>(sql, new { Id = id, StationId = id }, _connectionString);
public FullStationModel MultipleQuery<T, U>(string sqlStatement, U parameters, string connectionString)
{
FullStationModel output = new FullStationModel();
using (IDbConnection cnn = new SqlConnection(connectionString))
{
using (var multi = cnn.QueryMultiple(sqlStatement, parameters))
{
var stationinfo = multi.Read<BasicStationModel>().Single();
var stationcells = multi.Read<FullCellModel>().ToList();
output.BasicInfo = stationinfo;
output.Cells = stationcells;
}
}
return output;
}
I'm getting this:

I couldn't load the BasicInfo data inside of the objects of List<FullCellModel> and didn't find an example that combines Multiple Results with Multiple Mapping (I am assuming it could be the right approach but I don't know.)
How can I improve these lines?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MultipleQuery<T, U> method and its QueryMultiple call, then review Dapper's multiple-results and multi-mapping behavior. Verify that the station result and cell results can populate the requested FullStationModel, including each cell's BasicInfo, and confirm the returned object matches the stated model structure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100