DapperLib / DapperLib/Dapper

Multiple Results and Multiple Mapping

Open
#1,532 2 comments 0 reactions 0 assignees View on GitHub

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.

image

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:

image

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.