Predicate--Passing FieldIndex as parameter?

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 26-Feb-2005 02:57:30   

Hello,

Essentially, I want to abstract the predicate expression so I can pass in an array of values (string in this case), and create a bunch or OR predicates for it. I can then return the predicate expression to the main logic and AND it to the main predicate.

Here is what I am trying to do in code:

function:


protected IPredicateExpression
  CreateStringPredicate(string[] stringArray, int fieldEnum)
    {
      IPredicateExpression pred = new PredicateExpression();
      int x;
            
      for (x = 0 ; x < stringArray.Length ; x++)
      {
       pred.AddWithOr(MainFactory.PredicateFactory.Like(fieldEnum, "%" + stringArray[x] + "%"));                
      }
      return pred;
     }

calling the function:


CreateStringPredicate(myStringArray, MainDAL.CompanyFieldIndex.CompanyName));

This doesn't work. I get:

Argument '1': cannot convert from 'int' to 'CoAMS.DAL.ClaimActivityFieldIndex'

This one doesn't make sense. I don't know why it chose claimactivity to use in the error message.

I also get:

cannot convert from 'CoAMS.DAL.CompanyFieldIndex' to 'int'

This one makes more sense.

How can I pass the fieldindex as a parameter?

Thanks in advance,

Phil

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Feb-2005 11:08:14   

Using an int, makes the enums not going to work. The enums work because they're typed and the compiler knows which method to call. If you pass in an int, it doesn't know that.

So if your routine is for a given entity, pass in the field index as an enum. If you don't know that, but you do know the entity type (enum value), you can pass in that too (as EntityType), then create an EntityFields object using the EntityFieldsFactory.CreateEntityFieldsObject(entityType).

You then index in that object with fieldIndex to get the field object, and simply use the FieldLikePredicate constructor instead of the PredicateFactory class.

Frans Bouma | Lead developer LLBLGen Pro
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 17-Mar-2005 00:17:14   

Otis wrote:

Using an int, makes the enums not going to work. The enums work because they're typed and the compiler knows which method to call. If you pass in an int, it doesn't know that.

So if your routine is for a given entity, pass in the field index as an enum. If you don't know that, but you do know the entity type (enum value), you can pass in that too (as EntityType), then create an EntityFields object using the EntityFieldsFactory.CreateEntityFieldsObject(entityType).

You then index in that object with fieldIndex to get the field object, and simply use the FieldLikePredicate constructor instead of the PredicateFactory class.

Oy. I am still having a hard time with this.

(I am now using Adapter--not sure if that makes any difference.)

In both sections of code (the calling section and the function), I listed a commented way I would like it to work (so that a change in field names would cause compiler errors). But I can't get it to work the UNcommented way either.


predTemp = CreateStringPredicate(searchParam.ClientName, "ClientName", SecurityDAL.EntityType.ClientEntity);
//predTemp = CreateStringPredicate(searchParam.ClientName, SecurityDAL.ClientFieldIndex.ClientName, SecurityDAL.EntityType.ClientEntity);


//protected IPredicateExpression CreateStringPredicate(ArrayList stringArray, System.Enum fieldIndex,
// SecurityDAL.EntityType entityType)
protected IPredicateExpression CreateStringPredicate(ArrayList stringArray, string fieldName, SecurityDAL.EntityType entityType)
        {
            IPredicateExpression pred = new PredicateExpression();
            int x;

            //this part works
            IEntityFields2 eFields = SecurityFactory.EntityFieldsFactory.CreateEntityFieldsObject(entityType);
            
            //IEntityField2 fieldeFields[(int)fieldIndex];
            IEntityField2 field = eFields[fieldName];           
            
            for (x = 0 ; x < stringArray.Count ; x++)
            {
//the following line of code gives the error:
//Argument '1': cannot convert from 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityField2' 
//to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityField'
                pred.AddWithOr(new FieldLikePredicate(field, "%" + stringArray[x] + "%"));          
            }
            return pred;
        }



As always, thanks for any help.

Phil

netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 17-Mar-2005 10:22:51   

Have a read of this thread, which may be of help - http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2340

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Mar-2005 13:22:43   

change: pred.AddWithOr(new FieldLikePredicate(field, "%" + stringArray[x] + "%")); into: pred.AddWithOr(new FieldLikePredicate(field, null, "%" + stringArray[x] + "%"));

Frans Bouma | Lead developer LLBLGen Pro