[FIXED] problem with IndexOf ??

Posts   
 
    
mike
User
Posts: 24
Joined: 02-Oct-2003
# Posted on: 17-Oct-2003 21:26:50   

Hi, I was wondering if anyone could tell me if i'm doing something wrong here. I'm trying to find the index of an entity inside a collection. I first create an entity by specifying the key,

= new entity(key)

then calling collection

IndexOf(entity

and always getting a -1 return.

However if i create an entity by specifying the index of the collection and then placing it back into the IndexOf method it returns the correct index.

thanks mike

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 17-Oct-2003 22:11:40   

Weird error, Mike. IndexOf is overloaded, but it just does List.IndexOf(entityToFind);, and List is the collection (of CollectionBase, the base class of the collection classes).

I'll look into it.

Frans Bouma | Lead developer LLBLGen Pro
mike
User
Posts: 24
Joined: 02-Oct-2003
# Posted on: 17-Oct-2003 22:56:46   

Thanks!, I'm still not having any luck cry mike

mike
User
Posts: 24
Joined: 02-Oct-2003
# Posted on: 20-Oct-2003 22:53:19   

Can anyone tell me if I'm doing this wrong. I can't seem to determine if an entity is in a collection using the Contains or Index methods.

here is what i'm basically doing in my code:

Collection c = new Collection(); c.getMulti(null , 0)

Entity e = new Entity("test"); //where test is a key in the collection list

when I try to do a comparison

int = c.IndexOf(e);

or

int = c.Contains(e);

I'm always getting a -1.

am I using the function wrong in anyway?

thanks mike

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 20-Oct-2003 23:07:07   

Yes, you're using the function wrong sunglasses . Index Of and Contains look for instance equivalents. So:

MyEntityCollection foo = new MyEntityCollection(); foo.GetMulti(null); bool iAmTrue=foo.Contains(foo[0]);

will work, because it compares instances which of course works. You have a new instance, and then want to check if that instance is in the collection. The contents of your new instance is in the collection, but the instance itself is not. Therefore, will result in false/-1.

The collection should be extended with a hashtable which uses the PK values of an entity which is added to the collection, to find it back. This wil be added in November, at the moment there is no way you can find an entity back using the PK other than to walk the complete collection, which is probably not a very wise idea.

Frans Bouma | Lead developer LLBLGen Pro
mike
User
Posts: 24
Joined: 02-Oct-2003
# Posted on: 20-Oct-2003 23:49:17   

Thanks, that answers my questions. Would you have any suggestions as to how to implement this type of search for the moment? Perhaps throwing the results into a datagrid and trying to do searches that way using c# functions or perhaps subclassing one of your classes and adding our own search function?

thank you mike

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 21-Oct-2003 09:36:17   

mike wrote:

Thanks, that answers my questions. Would you have any suggestions as to how to implement this type of search for the moment? Perhaps throwing the results into a datagrid and trying to do searches that way using c# functions or perhaps subclassing one of your classes and adding our own search function?

If you need it now, you could overload Add() in the collection class, add a hashtable to the collection class (be sure to serialize it) and when Add() or Insert() is called, add the entity to the hashtable with its PK values. (separate multi-pk values with some separator). Then add a Find(pkvalue1,... pkvalueN) method which takes the pk values, and uses the hashtable to retrieve the entity.

Be sure to clear the hashtable when a getmulti* is executed, since those methods clear the contents.

In November I'll add this to the collection class templates.

I don't know what you want to do with the data you find as a result of the search. If it doesn't matter if the data is in an entity object or not, you could execute the GetMultiAsDataTable() method when retrieving the entity objects, and use that datatable with a dataview to search for rows and use that data.

However, I'd go back to the database.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 17-Nov-2003 14:43:48   

It appeared to be a bug in the Equals() implementation of IEntityFields. This bug is fixed with the new ORM support classes (17-nov-2003 release). Because this bug is now fixed, IndexOf works as expected: you can find an entity with the same Primary Key field values in an entity collection by using IndexOf and passing in an entity instance which has the primary key field values filled in.

Frans Bouma | Lead developer LLBLGen Pro