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?