DynamicQuery Project into Custom Class with List Property

Posts   
 
    
TopDog74
User
Posts: 40
Joined: 27-Apr-2012
# Posted on: 19-Feb-2015 11:53:16   

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
            
}

            

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Feb-2015 18:41:05   

Please check the examples in Hierarchical Sets

TopDog74
User
Posts: 40
Joined: 27-Apr-2012
# Posted on: 20-Feb-2015 12:22:37   

Thanks for the response. I have had a look, there are examples there of nested queries, BUT what i am really looking for and i apologize if this was not clear in my question is:

Can i create the QuerySpec to execute just 1 query (the join to to the relation is specified) and then populate the list property 'Addresses'.

If this is possible, is there an example somewhere of something very similar so i can see what the syntax is please?

Thanks, Iain

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Feb-2015 04:40:00   

You could create a DynamicList in QuerySpec and then create a projector. More info in tbe documentation.

David Elizondo | LLBLGen Support Team