reference entities in a collection before saving

Posts   
 
    
johnsmith
User
Posts: 19
Joined: 14-Dec-2004
# Posted on: 14-Dec-2004 17:55:05   

How do I reference an entity in a collection using some sort of string ID when the entities haven't been saved yet. For example, in a web application I have a collection stored in the session and populate a DropDownList with its content using DataBind. On postback I need to identify the selected item within the collection using the drop down's SelectedValue property. What entity property should I use for MyDropDown.DataValueField and how do I use this property to reference the corresponding entity in MyEntityCollection, given that at this stage, MyEntityId is 0 for each. MyEntity.ObjectID seems a likely solution but I can't see how to use it to retrieve an entity in a collection without looping.

Thanks, JS.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 14-Dec-2004 18:47:59   

You should use ObjectID, this is a GUID which is unique for each entity object. Finding the entity back without looping isn't possible, either you execute a loop inside the entitycollection or you loop yourself.

If you don't modify the collection, you can also store the collection in the session, and store the index in the collection as the datavalue for the dropdown (or calculate the index from the SelectedIndex property). As the collection isn't changed, the index is still valid.

Frans Bouma | Lead developer LLBLGen Pro
johnsmith
User
Posts: 19
Joined: 14-Dec-2004
# Posted on: 15-Dec-2004 11:24:48   

Thanks for your quick response Otis. Using the datagrid's SelectedIndex property works, but as you say that requires that the collection isn't changed. ObjectID doesn't seem to work with the binding however, inexplicably returning:

DataBinder.Eval: 'MyProject.DAL.EntityClasses.MyEntity' does not contain a property with the name ObjectID.

I'm stumped by this, each MyEntity returns its .ObjectID with no problem. Any ideas?

And while I'm at it, are any plans to build a hash-based lookup by ObjectID into EntityCollectionBase? Seems to me it's one of the first things you'd ever need in a collection, well, it was one of the first things I needed...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 15-Dec-2004 13:26:28   

johnsmith wrote:

Thanks for your quick response Otis. Using the datagrid's SelectedIndex property works, but as you say that requires that the collection isn't changed. ObjectID doesn't seem to work with the binding however, inexplicably returning:

DataBinder.Eval: 'MyProject.DAL.EntityClasses.MyEntity' does not contain a property with the name ObjectID.

I'm stumped by this, each MyEntity returns its .ObjectID with no problem. Any ideas?

Perhaps if you cast the entity to IEntity or EntityBase ?

And while I'm at it, are any plans to build a hash-based lookup by ObjectID into EntityCollectionBase? Seems to me it's one of the first things you'd ever need in a collection, well, it was one of the first things I needed...

A hashtable could be great, but filling a hashtable takes time, and if you don't need it, it's not something you want to waste cycles on. However indexes on columns in collections are planned.

Frans Bouma | Lead developer LLBLGen Pro
johnsmith
User
Posts: 19
Joined: 14-Dec-2004
# Posted on: 15-Dec-2004 16:35:24   

Don't see how a cast from MyEntity to IEntity or EntityBase fits in when we're just dealing with the collection, i.e.:

DropDownList1.DataSource=MyEntityCollection; DropDownList1.DataTextField="MyEntityID"; DropDownList1.DataValueField="ObjectID"; DropDownList1.DataBind();

It's all possible with loops of course but I was hoping there was a more elegant solution. Anyway, this issue appears to be a .NET 'feature' which I gather applies to any property of a collection member's base class, and I appreciate this is not a .NET support forum. Having said that, if anyone reading knows a way...!

Glad to see you've got collection indexes in the pipeline, and yes, you're right that hashtables would be a bit heavyweight for what would usually be very small temporary collections.

Thanks for your help. Great product. Keep it up!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 15-Dec-2004 17:17:35   

simple_smile

My understanding of the workings of DataBinder.Eval is taht it's not the smartest kid on the block. Especially if you use it in aspx databinding scenario's (with the <%# tags) it always requires casting (oh of course it magically recognizes datatables... )

Though, to give it some credit, I think it doesn't see the ObjectID field, as an entity implements ICustomTypeDescriptor, and it only adds non-hidden properties (i.e properties which don't have Browsable(false) applied to them). ObjectId is made hidden (so it doesn't pop up in grids)

Casting the object directly to IEntity in the <%# tag will help, but as you use a different method, I'm not sure how to use it in this case...

Frans Bouma | Lead developer LLBLGen Pro
johnsmith
User
Posts: 19
Joined: 14-Dec-2004
# Posted on: 04-Sep-2006 14:15:35   

Hi there. I just wondered if there is now a better way to do this in version 2? Just to recap - the problem is to retrieve an unsaved entity from a collection, where there is therefore no EntityId yet. The only solution in version 1 was to loop though the collection comparing ObjectIds.

Thanks, JS.

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 04-Sep-2006 17:00:29   

Hi,

with v2, you can "interpret" filters right on your entity collection by using the FindMatches(yourIPredicate) Methods from EnntityCollection

Further more, the EntityProperty class let you define predicate agianst non column based properties such as IsDirty / IsNew. You should find what you need with that system.