Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper to join records from 2 tables into single IEnumerable viewmodel

Tags:

c#

automapper

I have 2 Tables, say T1 and T2. T1 contains oID,cID,date,status and T2 contains cID,cName,cURL. I have designed class for above 2 tables as below:

T1.cs

public class T1{
  public int oID{get;set;}
  public int cID{get;set;}
  public DateTime date{get;set;}
  public string status{get;set;}
}

T2.cs

public class T2{
    public int cID{get;set;}
    public string cName{get;set;}
    public string cURL{get;set;}

}

cID in T1 is a foreign key referring T2 - cID

Now I have my T3 view model as below to combine T1 and T2

T3.cs

public class T3:T1
{
   public int cID{get;set;}
   public string cName{get;set;}
   public string cURL{get;set;}
}

T3 extends T1 and T2 properties are defined in T3. Thus I was intending to combine 2 tables in a single view model using AutoMapper. I have below method to get all details from T1 and related details from T2, which when filled will return IEnumerable T3.

public IEnumerable<T3> GetAll()
{
   var od = mycontext.t1repository.GetMany().ToList();
   var ck = myContext.t2repository.GetMany(x => od.All(y => y.cID == x.cID)).ToList();
   if (od.Any())
   {
        Mapper.CreateMap<tblT1, T3>();
        Mapper.CreateMap<tblT2, T3>()
                    .ForMember(x=>x.cID, a => a.MapFrom(s => s.cID))
        var response = Mapper.Map<List<T1>, List<T3>>(od);
        return response;
   }
   return null;
}

I tried the above code via this answer but that is for single instance and I have to return IEnumerable of records. I am not sure how I can actually map data's from 2 table based on their cID. Any idea how I can achieve this?

like image 365
Guruprasad J Rao Avatar asked Oct 18 '22 19:10

Guruprasad J Rao


2 Answers

I think you can try something like this:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<T1, T3>();
    cfg.CreateMap<T2, T3>();
});

var mapper = config.CreateMapper();

var od = new List<T1> { ... };
var ck = new List<T2> { ... };

var result = od.Join(ck, t => t.cID, t => t.cID, (t1, t2) =>
{
    var t3 = mapper.Map<T1, T3>(t1);
    t3 = mapper.Map<T2, T3>(t2, t3);

    return t3;
});
like image 152
Arturo Menchaca Avatar answered Oct 21 '22 08:10

Arturo Menchaca


First you do not need here cID because it will be inherited from base class T1:

public class T3:T1
{
   public int cID{get;set;}  //need to remove
   public string cName{get;set;}
   public string cURL{get;set;}
}

Then create a map T1 to T3:

   Mapper.CreateMap<T1, T3>()
    .ForMember(dest => dest.cName, opt => opt.Ignore())
    .ForMember(dest => dest.cURL, opt => opt.Ignore());

After that create a map for T2 to T3:

  Mapper.CreateMap<T2>, T3>()
   .ForMember(dest => dest.oID, opt => opt.Ignore())
   .ForMember(dest => dest.date, opt => opt.Ignore())
   .ForMember(dest => dest.status, opt => opt.Ignore());
like image 44
Roman Marusyk Avatar answered Oct 21 '22 08:10

Roman Marusyk