Using the entitycollection

Posts   
 
    
HenkO
User
Posts: 5
Joined: 15-Jun-2005
# Posted on: 15-Jun-2005 11:41:30   

Hello,

I’m a new LlBlGen user, at the moment I’m testing the demo version. I’m working on a data project with large tables, i.e. I have a lot of customers in a table which I want to load in memory. I can’t figure out how to select a customer by name out of an entitycollection. Is this possible? If so does any one have a simple example (I’m also a novice on VS2003).


‘Hereby the code I have been using before LlBlGen.
Dim MyDataSet As New DataSet
' Left out : Some code to fill the dataset
Dim strFilter As String = "name = 'Henk'"
Dim dtrFound As DataRow() = MyDataSet.Tables("Customer").Select(strFilter)
    If dtrFound.Length() > 0 Then
       ' No customers found
    Else
       For index As Integer = 0 To dtrFound.Length - 1
          ' Write out the names of the customers 
          Console.WriteLine(CType(dtrFound(index).Item("name"), String))
       Next
    End If

sunglasses Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 15-Jun-2005 12:12:46   

If you have a lot of customers in your table, it's perhaps not wise to load them all into memory, as that can be slow. (though, 'a lot' is perhaps subjective, I wouldn't load more than a 1000 customers in memory).

If you want to load a customer using a name, you can better do: (if name is a field with a unique constraintsimple_smile


Dim customer As New CustomerEntity()
customer.FetchUsingUCName("Henk")

This will then load the customer with name 'Henk' from the database. (if the field 'name' has a unique constraint of course).

If it doesn't have a unique constraint, you can define a filter:


Dim filter As Predicate = PredicateFactory.CompareValue(CustomerFieldIndex.Name, ComparisonOperator.Equal, "Henk")
CustomerCollection customers = new CustomerCollection()
customers.GetMulti(filter)

This will get all customers which will have 'henk' for the field 'name'. As you can see, I use the entity collection to fetch the objects, as there can be more than one of course.

You can use Find on a fetched entity collection, though that performs a linear search, which is the same as a forloop over the collection, checking customers(index).Fields(fieldIndex).CurrentValue or else: customers(index).Name

Frans Bouma | Lead developer LLBLGen Pro
HenkO
User
Posts: 5
Joined: 15-Jun-2005
# Posted on: 15-Jun-2005 13:23:42   

Thanks for your quick response, I have tried your suggestions but I can’t get them working.

Option 1.

Dim customer As New CustomerEntity()
customer.FetchUsingUCName("Henk")

The customer object has no Fetch… method

Option 2.

Dim filter As Predicate = PredicateFactory.CompareValue(CustomerFieldIndex.Name, ComparisonOperator.Equal, "Henk")
CustomerCollection customers = new CustomerCollection()
customers.GetMulti(filter)

I don’t have a Customer collection

During the code generation I receive the following messages : Could not find template 'SD_ActionProceduresAdapterTemplate'. It is not defined in the template set config file. Could not find template 'SD_RetrievalProceduresAdapterTemplate'. It is not defined in the template set config file.

I’m working on a MsAccess 2003 database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 15-Jun-2005 14:01:27   

HenkO wrote:

Thanks for your quick response, I have tried your suggestions but I can’t get them working.

Ok, I should have mentioned that the examples were for 'SelfServicing'. I assume you generated code for 'Adapter' then. We support two different paradigms: selfservicing (which has the persistence logic in the entities (like customer.Save()) and adapter which has teh persistence logic in a separate object (adapter.SaveEntity(customer)).

Option 1.

Dim customer As New CustomerEntity()
customer.FetchUsingUCName("Henk")

The customer object has no Fetch… method

Correct, you have to do for adapter:


Dim customer As New CustomerEntity()
customer.Name = "Henk"
Dim adapter As New DataAccessAdapter()
adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCName())

That is, if Name has a unique constraint of course.

Option 2.

Dim filter As Predicate = PredicateFactory.CompareValue(CustomerFieldIndex.Name, ComparisonOperator.Equal, "Henk")
CustomerCollection customers = new CustomerCollection()
customers.GetMulti(filter)

I don’t have a Customer collection

Correct, Adapter has untyped collections. For adapter you do this:


Dim adapter As New DataAccessAdapter()
Dim filter As New RelationPredicateBucket()
filter.PredicateExpression.Add(PredicateFactory.CompareValue(CustomerFieldIndex.Name, ComparisonOperator.Equal, "Henk"))
EntityCollection customers = new EntityCollection(new CustomerEntityFactory())
adapter.FetchEntityCollection(customers, filter)

During the code generation I receive the following messages : Could not find template 'SD_ActionProceduresAdapterTemplate'. It is not defined in the template set config file. Could not find template 'SD_RetrievalProceduresAdapterTemplate'. It is not defined in the template set config file.

I’m working on a MsAccess 2003 database.

That's normal, stored procedures on access aren't supported, so the template isn't defined. You can safely ignore that error.

Frans Bouma | Lead developer LLBLGen Pro