Dyanamic sort fields

Posts   
 
    
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 17-Dec-2010 16:11:12   

Using self servicing with version 2.6

I am trying to create a dynamic sort clause using the name of the field being passed in.

So this works for basic sort expressions:


string FieldName= "Title";
this.SortExpression.Add(new SortClause(new EntityField(FieldName, new Expression()), SortDirection));

Quite often I need to sort on a related field's related field (e.g. Account > User > Contact Details) and creating a new EntityField isn't working for that. Using a normal entityfield (i.e. ContactFields.FirstName) works fine.

So my question is, how do you get an entity field from a string with the field name? Is it possible to get the entity type as well?

Something like this:


string EntityName = "Contact";
string FieldName = "FirstName";
SortClause Sort = new SortClause(MagicFunction(EntityName, FieldName), SortOperator.Ascending)


trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 17-Dec-2010 16:15:13   

I can do this

new SortClause((EntityField)EntityFieldFactory.Create(ContactFieldIndex.FirstName)

but it still doesn't help with dynamically getting the field index.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Dec-2010 22:44:31   
David Elizondo | LLBLGen Support Team
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 20-Dec-2010 10:00:41   

Thanks but that didn't work as the collection is not present.

I did find the solution though. Had I not been lazy and actually gone through all the create overloads I would have noticed this little beauty right at the end


EntityFieldFactory.Create(string objectName, string fieldName)

In case anyone finds this, objectName is the enity e.g. CustomerEntity and fieldName is the field.