Hi, I am trying to project the results from a DynamicQuery into a custom class (Data Transfer Object). I have done this before for simple projections into simple classes with basic property fields, but i don't know if it is possible to project into a custom class which contains a List<MyType> property.
I have example code below of what i am trying to do. Given my PersonDto class, i am trying to project directly from QuerySpec into this type which contains the List<> property called 'Addresses'
Could you tell me if this is possible?, and if it is possible what the correct syntax is please.
Thanks.
public class PersonDto
{
public int PersonId {get; set;}
public string Fullname {get; set; }
public List<AddressDto> Addresses {get; set;}
}
public class AddressDto
{
public int AddressId {get; set;}
public string City {get; set;}
}
//Service layer method to get PersonDto by identifier
public PersonDto GetPersonById(int id)
{
var qf = new QueryFactory();
DynamicQuery<PersonDto> query = null;
query = qf.Create()
.Select(() => new PersonDto
{
PersonId = PersonFields.PersonId.ToValue<int>(),
Fullname = PersonFields.Fullname.ToValue<string>(),
Addresses = 'what to do here' => new AddressDto
{
AddressId = AddressFields.AddressId.ToValue<int>(),
City = AddressFields.City.ToValue<string>()
})
}).From(qf.Person
.InnerJoin(qf.Address).On(AddressFields.PersonId == PersonFields.PersonId)
);
query = query.Where(PersonFields.PersonId == id);
//rest of this method is not relevant to the question
}