Sample using "Find" method

Posts   
 
    
jeffkararo
User
Posts: 76
Joined: 06-Jan-2004
# Posted on: 18-Feb-2004 16:48:09   

Does anyone have some sample code they can share using the "Find" method of a collection object? I have a collection of objects and I want to "find" one with a particular ID.

Thanks, Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Feb-2004 17:04:02   

CustomerEntity customerChops = (CustomerEntity)customers[customers.Find(new EntityPropertyDescriptor2( EntityFieldFactory.Create( CustomerFieldIndex.CustomerId),typeof(CustomerEntity), false), "CHOPS")];

Remember, the Find method is the IBindingList.Find method, thus a normal .NET method. Behind the scenes it does a linear search, so you can also do this yourself:


IEntity foundEntity = null;
for(int i=0;i<myCollection.Count;i++)
{
    if(myCollection[i].Fields["FieldToSearch"].CurrentValue==valueToSearchFor)
    {
        foundEntity = myCollection[i];
        break;
    }
}

Frans Bouma | Lead developer LLBLGen Pro
jeffkararo
User
Posts: 76
Joined: 06-Jan-2004
# Posted on: 18-Feb-2004 17:16:33   

Thanks for the code snippet. What is "EntityPropertyDescriptor2"? Is it a part of some namespace that I am unfamiliar with?

Thanks Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Feb-2004 17:28:26   

jeffkararo wrote:

Thanks for the code snippet. What is "EntityPropertyDescriptor2"? Is it a part of some namespace that I am unfamiliar with?

Thanks Jeff

It's a class in the ORMSupportclasses lib, derived from PropertyDescriptor. It is required to describe a property of a class to a bound control using complex databinding (ITypedList.GetItemProperties is the nice little gem this stuff is required for).

I implemented the Find method so complex bound controls can use this method as it is a method of IBindingList (so find a value in a column of a grid for example). It's not meant for manual search through the properties as it is cumbersome to set up with that property descriptor.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 20-Feb-2004 11:54:35   

This looks as though its using the self serving capabilities. How does one do this with the adapter.

I have a collection of users that are returned via a predicate bucket expression


            IPredicateExpression filter = new PredicateExpression();
            filter.Add(PredicateFactory.CompareValue(DAL.UserFieldIndex.UserEmail,ComparisonOperator.Equal,email));
            filter.AddWithAnd(PredicateFactory.CompareValue(DAL.UserFieldIndex.UserPassword,ComparisonOperator.Equal,password));
            filterbucket.PredicateExpression.Add(filter);

            broker.FetchEntityCollection(users,filterbucket);

This returns the collection of users who meet that requirement. In my case, it should always be 1 only. However I'm trying to figure out

  1. if there is an easy way to determin if a user is found successfully and
  2. if so, create the UserEntity object and if not
  3. do something else
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 20-Feb-2004 17:55:11   

If email is unique, you could define a unique constraint and you can fetch the entity using that unique constraint.

Find works in both selfservicing and adapter, but as I said, it does a linear search so you can easily perform that search yourself.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 20-Feb-2004 18:35:05   

Thanks, here's what I did

    IPredicateExpression filter = new PredicateExpression();
            filter.Add(PredicateFactory.CompareValue(DAL.UserFieldIndex.UserEmail,ComparisonOperator.Equal,email));
            filter.AddWithAnd(PredicateFactory.CompareValue(DAL.UserFieldIndex.UserPassword,ComparisonOperator.Equal,password));
            filterbucket.PredicateExpression.Add(filter);

            broker.FetchEntityCollection(users,filterbucket);
            if(users.Count != 0)
            {
                Console.WriteLine(users.Count.ToString());
                UserEntity loginUser = (UserEntity)users[0];
                Console.WriteLine("UserID: " + loginUser.UserId);
                //Add to sessionstate
            }
            else
            {
                Console.WriteLine("Login failed");
            }

Which seems to work fine. Just wondering if its the best thing to do. The email is unique and I was thinking about using the unique constraint capabilities as well. If I were to use the unique constraint I would do something like the following:

  1. Grab the userentity based on the unique constraint
  2. Validate that the password for the userentity is correct
  3. if valid user, do something else do something else

Is one way more efficient than the other.