Is It possible to populate a List from within a EF query? My DTO is as follows:
public DTO()
{
public string ClientName { get; set;}
public List<string> RelatedCodes { get; set;}
}
My method to populate DTO =
Public DTO MyResult(string ClientCode)
{
return (from o in repository.RelatedClient
where o.LeadCode == ClientCode
select new DTO { ClientName = o.ClientName, RelatedCodes = o.RelatedCodes.ToList()});
}
I know I shouldn’t be adding the ToList() within the query or if I can I am doing it very wrong. Can anyone advise how this is done. My DTO is cut down for this example but I want to have lots of single properties and a List<> (or several Lists) in a DTO with one Entity query if possible?
So following answer below I presume there isnt a way to query this direct. My original option would have been:
Public DTO MyResult(string ClientCode)
{
var temp = (from o in repositry.RelatedCode Where o.LeadCode == ClientCode select o).ToList();
DTO dto = (from o in repository.LeadClient Where o.LeadCode == ClientCode Select o.ClientName).Firstordefault();
foreach(string rc in temp)
{ dto.RelatedCode.Add(rc);}
return dto;
}
I am happy with this but would be grateful if someone could advise if this is the correct way to deal without something like automapper. I am trying to learn Entity Framework and want to check I am not missing some built in functionality.
Something like:
Using this approach you can add as many properties as you’d like. However, I’d suggest using AutoMapper for this.
Read more here:
http://nuget.org/packages/automapper