Dynamically specify FieldIndex in Predicate.

Posts   
 
    
the_narv avatar
the_narv
User
Posts: 8
Joined: 15-Dec-2004
# Posted on: 16-Feb-2005 13:11:46   

Hi,

This is more of a C# question than directly related to LLBLGen Pro, but I am hoping someone here will shed some light on it for me.

What I want to do is to be able to dynamically specify at runtime the FieldIndex to use as a parameter to the PredicateFactory.Create method.

For example,

IPredicatExpression Filter = new PredicateExpression( PredicateFactory.Create( TableFieldIndex.Column1, ComparisonOperator.Equal, 1 ) );

It is the TableFieldIndex.Column1 bit that I need to dynamically create at runtime, but I am having trouble doing this. At runtime I will know the table name required, and I will also know the suffix of the column name so I am hoping I can dynamically create the TableFieldIndex enum and look for a column within that matches my suffix, then pass that as the parameter to PredicateFactory.Create.

Any advice anyone can give would be most appreciated.

Regards,

Adam.

netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 16-Feb-2005 13:29:02   

Do you have access to the entity collection at this point?

If you do then you can use it to get a fields collection...

IEntityFields2 fields = myEntityCollection.EntityFactoryToUse.CreateFields();

you can then use the actual field (after finding it in the fields collection using your field name) to create the predicate yourself...

FieldCompareValuePredicate compare = new FieldCompareValuePredicate(myField, null, ComparisonOperator.Equal, myValue);

I originally tried and failed to do it the way you're trying by using the field index enum.

Hope this helps, Adrian.

p.s. i wrote a function to create a collection give a table name, something like this....


        public static EntityCollection CreateCollection(string tableName)
        {
            // build a string containing the name of the entity factory
            string factoryName = tableName + "EntityFactory";
        
            EntityCollection ents = null;

            // Get a reference to the current assembly
            // build a string containing the fully qualified name of the entity factory
            //      using the business objects namespace 
            //              + the factoryName
            // create an instance of the EntityFactory
            // use the EntityFactory to create a new EntityCollection

            Assembly ass = Assembly.GetExecutingAssembly();
            IEntityFactory2 fac = (IEntityFactory2)ass.CreateInstance("MyRootNameSpace.BusinessObjects." + factoryName, true);
            if (fac != null) ents = new EntityCollection(fac);
            return ents;
        }

the_narv avatar
the_narv
User
Posts: 8
Joined: 15-Dec-2004
# Posted on: 16-Feb-2005 15:02:38   

Adrian,

That worked perfectly.

Thank you very much for your advice.

Regards,

Adam.

smile