-
How can I map an object from a database where I have an Example: Using dapper I populate this: public record DbPage
{
public int ParentId { get; set; }
public string ParentName { get; set; }
public int? ChildId { get; set; }
public string? ChildName { get; set; }
} Where each row consist of:
And I have in the business layer this public record Page
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<Page> Children { get; set; }
} I tried: [Mapper]
public partial class MyMapper
{
[MapProperty(nameof(DbPage.ParentID), nameof(Page.ID))]
[MapProperty(nameof(DbPage.ParentName), nameof(Page.Name))]
public partial Page FlatToPage(IEnumerable<DbPage> flat);
} I also tried adding a user implemented mapping: private Page DbParentRowToPage(IEnumerable<DbPage> parent)
{
var group = from p in parent
group p by p.ParentID into singleParent
let children = singleParent.Select(x => new Page
{
ID = x.ChildID.GetValueOrDefault(),
Name = x.ChildName ?? ""
}).ToList()
let first = singleParent.FirstOrDefault()
where first is not null
select new Page
{
ID = first.ParentID,
Name = first.ParentName,
Children = children
};
return group.Single();
} But I get |
Beta Was this translation helpful? Give feedback.
Answered by
latonz
May 28, 2024
Replies: 1 comment 2 replies
-
What does your generated code look like? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to your sample data this looks like a group by task which is not well suited in an object to object mapper like Mapperly...