Find Entity from Collection by PK

Posts   
 
    
jader201
User
Posts: 33
Joined: 20-Mar-2007
# Posted on: 09-Jul-2007 23:33:05   

LLBLGen Pro v. 2.0 SelfServicing

What is the suggested method for getting to a particular entity within a collection by the primary key?

I have read about EntityViews, and if I understand EntityViews (which I very well may not), it seems like an over-complicated way to get to a particular entity. For example, I have to basically manually re-create the entire structure of the entity through a collection of EntityPropertyProjectors, and then fill a new collection using this EntityPropertyProjector collection.

Is there no way to directly access an entity through the filtered results of an EntityView, without having to project it onto a new collection? If not, is there a simpler way to project an EntityView onto an EntityCollection without having to manually recreate the entire structure of the entity (which is the same structure as the derived collection)?

It's possible I'm going about this completely wrong, and if so, I apologize. I'm still new at LLBLGen Pro and am still trying to make sense out of everything.

Thanks in advance.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Jul-2007 03:19:48   

Hi jader.

What is the suggested method for getting to a particular entity within a collection by the primary key?

Have you take a look on EntityCollection.FindMatches(IPredicateExpression)? I personally love this method and I use it a lot simple_smile . Here is a little example:

// you have a filled collection
CustomersCollection customers = new CustomersCollection();
customers.GetMulti(null);

// now you want to find some entity at in-memory collection
// this will retrieve a list of #'s indicadint the coincidences of the predicate.
List<int> i = customers.FindMatches(CustomersFields.CustomerId == "ALFKI");
CustomersEntity myCustomer = null;

// we found at least one
if (i.Count > 0)
{
    // we know the PK only will retrieve one (the first position)
    myCustomer = customers[i[0]];
}

Above code could be simplified in one line wink . I don't know if this is what you are looking for. Please let us know if we can help you in other way.

David Elizondo | LLBLGen Support Team
jader201
User
Posts: 33
Joined: 20-Mar-2007
# Posted on: 10-Jul-2007 14:35:26   

Thanks for the response, daelmo.

I actually found that listed in the documentation, not too long after I posted this (even though I had been working with this and searching for the solution for several hours). This seems to work in my situation, though I noticed that the documentation seems to favor using the EntityView:

Finding entities inside a fetched entity collection Although it's recommended to use EntityView objects to filter and sort an in-memory entity collection object, it sometimes can be helpful to just have a quick way to find in an in-memory entity collection an entity or group of entities matching a filter.

Do you know in what situations I should use an EntityView over this method? The documentation doesn't seem completely clear as to the purpose and benefits of using the EntityView for filtering and sorting, over the built-in EntityCollection methods (FindMatches and Sort).

Thanks again for your help.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 10-Jul-2007 16:03:41   

Hi,

if you just want to find entities to show them in a form and always the sames query, it's better to use an entity view. EntityView actually doesn't touch the data inside the EntityCollection. If you need to have different query and want to modify entity and save them, it's better to use entitycollection.

jader201
User
Posts: 33
Joined: 20-Mar-2007
# Posted on: 10-Jul-2007 18:15:53   

Ok, thanks jbb.

I've searched and found the missing link and the source of my confusion. When I first tried to use the EntityView, I wasn't sure how to access the filtered collection -- this wasn't clear in the documentation.

For example, I have this:

CustomerCollection customers = new CustomerCollection();
customers.GetMulti(null); // fetch all Customers
EntityView<CustomerEntity> customerView = new EntityView<CustomerEntity>(customers);
IPredicate filter = (CustomerFields.Country == "UK");
customerView.Filter = filter;

costomerView isn't an EntityCollection, and customerView.RelatedCollection just referenced the original (unfiltered) collection. So I thought I must project this onto a new EntityCollection, requiring me to set up all of the EntityPropertyProjection objects for every field in the collection (which seemed redundant to me).

I just now found the ToEntityCollection method on the EntityView:

CustomerCollection filteredCustomers = (CustomerCollection)customerView.ToEntityCollection();

However, this isn't covered at all in the documentation. To me, this seems like it would be a common task, but I just happened upon it by chance. Is there a reason this isn't covered under the "Generated code - Using the EntityView class" topic? Is this a deprecated method, and I should go about accomplishing this another way? Or, is what I'm trying to do not something that is done very often?

Thanks again for the help everyone.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 11-Jul-2007 10:24:39   

I just now found the ToEntityCollection method on the EntityView:

Code: CustomerCollection filteredCustomers = (CustomerCollection)customerView.ToEntityCollection();

However, this isn't covered at all in the documentation. To me, this seems like it would be a common task, but I just happened upon it by chance. Is there a reason this isn't covered under the "Generated code - Using the EntityView class" topic? Is this a deprecated method, and I should go about accomplishing this another way? Or, is what I'm trying to do not something that is done very often?

ToEntityCollection, is not a deprecated method. For sure you can use it. Most probably it was missed out in the documentation.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 11-Jul-2007 10:43:44   

Not every feature is covered in the documentation unfortunately, as the docs then would become twice as big I think. the ToEntityCollection method is described in brief in the reference manual when you check the members of EntityView and EntityView2. It's similar to the MSDN documentation where not every feature of the .NET framework is described with documentation: a lot is simply described in brief in the reference manual.

We added it to get the features DataView has, which can project the results onto a new datatable without problems, so we added this method for convenience, though we didn't expect that it would be used that much. simple_smile .

So check the reference manual as well for methods / properties on classes you're using simple_smile If you've trouble installing the reference manual inside vs.net, we've also a .chm version available to you online in our customer area.

Frans Bouma | Lead developer LLBLGen Pro
jader201
User
Posts: 33
Joined: 20-Mar-2007
# Posted on: 11-Jul-2007 17:03:26   

Great, thanks again for the help guys.

Yeah, I knew that the documentation was meant as more of a "getting started" guide, whereas the reference is meant to be a more complete reference of all classes and members. I guess it seemed that taking a collection and then generating a second, filtered collection would've been common and thus mentioned in the guide. However, what we've been working on is a sample "tutorial" app for our company, and we may discover better designs once we start on real-life applications.

I'll try installing the reference now. Thanks again.