Derived Models - predicates on Nested Elements

Posts   
 
    
usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 12-Mar-2018 20:43:22   

LLBLGen: 5.3.3 .net 4.6.1 Adapter Template Postgres 9.4

Looking at this example for fetching derived models:

List<Customer> results = null;
using(var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    var q = (from c in metaData.Customer
             where c.VisitingAddressCountry == "USA"
             select c)
            .ProjectToCustomer();
    results = q.ToList();
}

from this link: https://www.llblgen.com/Documentation/5.3/Derived%20Models/dto_llblgen.htm#fetching-dto-instances

This is pretty straightforward. But how would you handle a situation where the customer has a related Address table. The derived model would have a Multi-element set and you'd want to query all customers with an Address in "USA". How would this query be written?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 13-Mar-2018 08:42:26   
List<Customer> results = null;
using(var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    var q = (from c in metaData.Customer
             where c.Address.Country == "USA"
             select c)
            .ProjectToCustomer();
    results = q.ToList();
}

You write the query you want, the ProjectToCustomer simply transforms the resultset to a set of DTOs. If you have a nested Address element in the Customer, that's OK, it will fetch the related address elements for the customers in the resultset (which all are from 'USA' as the predicate filtered them out)

Frans Bouma | Lead developer LLBLGen Pro