Added relation during FetchTypedList

Posts   
 
    
Jamanga
User
Posts: 93
Joined: 21-Jul-2006
# Posted on: 22-Feb-2017 05:22:38   

Hi

I'm using LLBLGen 4.2

I was wanting to optionally add relations at runtime prior to a FetchTypedList call. The only way I could see to do this was via the following where I added the relations to a RelationPredicateBucket and it came back in the DataTable object.

This is ok... but I'd prefer to do it via a populated typedlist, so take advantage of all the typing + classes.

Populating via datatable:

// get current relations
var orderLocations = new OrderLocationTypedList();
var filter = new RelationPredicateBucket()
var relations = (RelationCollection)orderLocations.GetRelationInfo().Relations;
foreach (var thisRelation in relations) {
    filter.Relations.Add((IEntityRelation)thisRelation);
}

if (somecondition) {
    // add extra relations for extended condition
    filter.Relations.Add(SalesOrderLinesEntity.Relations.SalesOrderEntityUsingOrderNo, "", "", JoinHint.Inner);
    filter.Relations.Add(ProductEntity.Relations.SalesOrderLinesEntityUsingProductNumber, "", "", JoinHint.Inner);
                            
    filter.PredicateExpression.Add(ProductFields.Department == new string [] { "someval1", "someval2" });
}

// fetch data into datatable
var fields = orderLocations.GetFieldsInfo();
var results = new DataTable();
adapter.FetchTypedList(fields, results, filter, false);

I would prefer to call it filling the typedlist, but could only do this where filter = PredicateExpression rather than the Bucket, and couldn't see how to add relations at runtime. Am I missing something obvious?

var orderLocations = new OrderLocationTypedList();
var filter = new PredicateExpression();
// add some conditions
adapter.FetchTypedList(orderLocations, filter, 0, null, false, 0, 0);
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 22-Feb-2017 17:51:16   

You don't need to loop and copy the relations.

Just use TL.GetRelationInfo(); to get hold on a reference to the relation predicate bucket. Directly add relations and filters to it, and fetch the TL.

Jamanga
User
Posts: 93
Joined: 21-Jul-2006
# Posted on: 22-Feb-2017 22:26:18   

Hi Walaa

Thanks for the quick response, I can see how GetRelationInfo() returns a PredicateRelationBucket, which is better, but still can't see how to use it to populate my TypedList?

    var orderLocations = new OrderLocationTypedList();
    var filterBucket = orderLocations.GetRelationInfo();
            
    if (somecondition) {
        // add the product order and order line relations
        filterBucket.Relations.Add(SalesOrderLinesEntity.Relations.SalesOrderEntityUsingOrderNo, "", "", JoinHint.Inner);
        filterBucket.Relations.Add(ProductEntity.Relations.SalesOrderLinesEntityUsingProductNumber, "", "", JoinHint.Inner);
        
        filterBucket.PredicateExpression.Add(ProductFields.Department == "001");
    }
            
    adapter.FetchTypedList(orderLocations, filterBucket?); <- I can't see a "fetchtypedlist" method which populates the typedlist and accepts a RelationPredicateBucket param?
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Feb-2017 06:34:17   

Jamanga wrote:

I can't see a "fetchtypedlist" method which populates the typedlist and accepts a RelationPredicateBucket param?

Use the ones that accepts fieldCollectionToFetch as first parameters. Although in your code you don't need to pass this, just pass the additional filter. See this example, next two methods are equivalent, and those two have other variants that accepts sorter, allowDuplicates, etc params.

[TestMethod]
public void FetchTypedList()
{
    // tpl
    var tpl = new OrdersResumeTypedList();

    // filter
    var filter = new PredicateExpression(OrderFields.ShipCountry == "USA");

    // fetch
    using (var adapter = new DataAccessAdapter())
    {
        adapter.FetchTypedList(tpl, filter);
    }           
}

[TestMethod]
public void FetchTypedList2()
{
    // tpl
    var tpl = new OrdersResumeTypedList();

    //filter
    var bucket = tpl.GetRelationInfo();
    bucket.PredicateExpression.Add(OrderFields.ShipCountry == "USA");

    // fetch in a separate data table
    var results = new DataTable();
    using (var adapter = new DataAccessAdapter())
    {
        adapter.FetchTypedList(tpl.GetFieldsInfo(), results, bucket);
    }
}

For more information Check the docs about this topic.

David Elizondo | LLBLGen Support Team
Jamanga
User
Posts: 93
Joined: 21-Jul-2006
# Posted on: 23-Feb-2017 09:01:06   

Hi Daelmo

Thanks for the info - I had already tried the 2nd method (as I needed to use the bucket to send through the extra runtime relation details), however this method returns the data within a Datatable, rather than the strongly typed list, and the populated typedlist is what I am after - is there a way to do this?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Feb-2017 23:32:16   

Use the TypedList instead of the DataTable, as the TypedList inherits from DataTable.

var tpl = new CustomerOrdersTypedList();

var bucket = tpl.GetRelationInfo();
bucket.Relations.Add(OrderEntity.Relations.OrderDetailEntityUsingOrderId);
bucket.Relations.Add(OrderDetailEntity.Relations.ProductEntityUsingProductId);

bucket.PredicateExpression.Add(ProductFields.ProductId == 1);

using (var adapter = new DataAccessAdapter())
{
    adapter.FetchTypedList(tpl.GetFieldsInfo(), tpl, bucket);
}

Note I've used the FetchTypedList overload that accepts a DataTable, but I've passed the the TypedList instead.

Jamanga
User
Posts: 93
Joined: 21-Jul-2006
# Posted on: 23-Feb-2017 23:52:07   

Hi Walaa

Thank you so much, that is what I was missing!