Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

mapping domain entities to dto's

$
0
0

I have a complex dto that consists of records from 4 tables

public class AggregateDto
{
   public OrganisationDto Organisation { get; set; }
   public List<Meter.MeterDto> Meters { get; set; }
   public List<Site.SiteDto> Sites { get; set; }
   public List<Contracts.ContractDto> Contracts { get; set; }
}

Im then creating a linq to sql query to return data for a specified organisation using its id, so there will be 1 organisation and 0 or more meters, sites and contracts.  I cant figure out how to 'map' the list domain object to its corresponding list dto, heres the linq code

var query = await (from organisation in DbContext.Organisations
  where organisation.ID == request.Id
  join site in DbContext.Sites on organisation.Orgid equals site.Orgid
  into sites_joined
  from site_joined in sites_joined.DefaultIfEmpty()
  from meter in DbContext.Meters.Where(var_meter => site_joined == null ? false : var_meter.SiteLocationID == site_joined.SiteLocationID).DefaultIfEmpty()
  join contract in DbContext.Contracts on organisation.Orgid equals contract.orgid
  into contracts_joined
  from contract_joined in contracts_joined.DefaultIfEmpty()
  select new AggregateDto
  {
     Organisation = new OrganisationDto{ ID = organisation.ID, OrganisationName = organisation.OrganisationName },
     Sites = --what goes here ?? ,
     Meters = --and here,
     Contracts = -- and here
  }).FirstOrDefaultAsync();

the organisation dto was easy enough as its just a single entity, but sites, meters and contracts  are lists of dto objects, how do i map the domain objects in this code above ?  Am I even going about this the right way ?


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>