C
C#3y 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; }
}
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 };
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?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server