Click here to Skip to main content
14,927,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 4 classes namely ClassA, ClassADto, ClassAA(inner class to ClassA) and the final Result class.


public class InnerA
{
public int HouseNumber{get;set;}
public string StreetName{get;set;}
public string State{get;set;}
}

public class A
{
public int Age{get;set;}
public string Name{get;set;}
public InnerA InnerAObj[get;set;}
}

public class ADto
{

 public int Age{get;set;}
 public string Name{get;set;}

}

class Result
{
public string StreetName{get;set;}
public int TotalCount{get;set;}
public int TodaysDate{get;set;}
public List<ADto> AObjectsList{get;set;}
}



Now my aim is map the 'Result' class with the List of ClassA object to fill it the property 'AObjectsList' as below: Result data= map mapper.map>(obj);

Also at the same time in automapper i want to use custom function either using 'Resolve' or 'AfterMap' to set properties like 'TodaysDate' to current datetime of system and property 'TotalCount' by counting the number of data.

I tried in many ways using 'CreateMap' and also used 'ForMembers' as from 'classAA' we only need the 'StreetName' but it didn't work. Need some help please.


What I have tried:

1. I have tried plain CreateMap.
2. I tried to explicit mapping with .ForMembers for class InerA StreetName
3.I have tried .AfterMap for setting systemDatetime and count of recors.

But I am unable to manage the List of stuffs.
Posted
Updated 1-Jun-20 20:49pm

Mapper solution is not with me right now.
   
Comments
Maciej Los 2-Jun-20 2:09am
   
This is not an answer. Please, delete it to avoid down-voting.
If you want to add some information to your question, use "Improve question" widget.
This is not an answer, but i have too many doubts to post it as a comment.

1. I don't understand the reason of creating two classes containing almost the same data, where an ADto should be a base class for A. Take a deeper look at them:



C#
public class A
{
  public int Age{get;set;} //property exists in A class
  public string Name{get;set;} //property exists in A class
  public InnerA InnerAObj{get;set;} //new property
}
C#
public class ADto
{
 public int Age{get;set;}
 public string Name{get;set;}
}


For example: the base class of dog and snake is animal. They have few common properties, such as gender, family, eyes, etc. But they also differ: dog has got hair and snake has not; dog can walk, snake - crawl. Got it?

Please, read about Inheritance - C# Programming Guide | Microsoft Docs[^] and Polymorphism - C# Programming Guide | Microsoft Docs[^]

2. I do not see direct relationship between Result class and A or ADto... There's a StreetName field which belongs to InnerA. So, i have no idea how do you want to "connect" (map) them together.

3. Seems that Result class should contain a list of A class. If so, then you'll never need to create TotalCount field, as a list already have one.

When you resolve above issues and clarify all doubts, maybe you'll be able to create proper classes and relationships between them.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900