C#C
C#4y ago
AK sanTari

Dapper map to multiple Dictionaries

I'm using Dapper and .NET core 6 and am trying to map an object with multiple dictionaries in one sql query... is it possible?

The object looks like the following:
public class MasterDataModel
{
    public IEnumerable<KeyValuePair<int, string>>? Titles { get; set; }
    public IEnumerable<KeyValuePair<int, string>>? Genders { get; set; }
    public IEnumerable<KeyValuePair<int, string>>? Countries { get; set; }
}


I can easily make multiple calls and map them (as follow) but was looking for something nicer.

var titles = await Task.FromResult(ExecuteQuery(q => q.Query<dynamic>("SELECT * FROM Mst.Title"))
    .ToDictionary(r => (int)r.TitleID, r => (string)r.TitleValue));

return new MasterDataModel() { Titles = titles };


Being using Slapper for other repo calls but can't find online anything for Dictionaries. Any idea?
Or can a stored procedure do that and Dapper detect and map the object?
Was this page helpful?