Is this possible (Sort & Filter)

Posts   
 
    
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 19-Apr-2004 22:02:23   

I'm trying to write a method that accepts a column(field) name as a parameter and returns the appropriate field entity (ie. MyTableFieldIndex.Id). The following code doesn't work but kind of shows what I'm getting at:


private static IEntityField FindMyTableField( string FieldName )
{
    IEntityField sortField;

    switch( FieldName )
    {
        case "Id": default:
        {
            sortField = (IEntity)MyTableFieldIndex.Id;
            break;
        }
        case "Name":
        {
            sortField = (IEntity)MyTable;
            break;
        }
...

         return sortField;  
    }

}

I know I'm using IEntityField incorrectly since it won't compile as is. I've tried passing through a generic object with no luck either. Any ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 20-Apr-2004 09:56:07   

Are you looking for a way to create new fields or is this a method inside an entity ? If it's the latter, you can use myEntity.Fields[name] to retrieve a field. Otherwise you could use System.Enum.Parse and get the enum value back for a "EntityFieldIndex.FieldName" string, and that enum you can then use to fabricate a new entity field using the EntityFieldFactory class simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 20-Apr-2004 17:04:36   

Otis wrote:

Are you looking for a way to create new fields or is this a method inside an entity ? If it's the latter, you can use myEntity.Fields[name] to retrieve a field. Otherwise you could use System.Enum.Parse and get the enum value back for a "EntityFieldIndex.FieldName" string, and that enum you can then use to fabricate a new entity field using the EntityFieldFactory class simple_smile

It's for a method in a manager class I'm trying to create, but I missed the entity.fields[], which would mean I don't need to write my own simple_smile I don't see a fields collection in my TypedViews, is there an equivalent?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 20-Apr-2004 18:19:20   

You should call GetFieldsInfo() simple_smile

(Adapter. SelfServicing doesn't have this method, you should then use: IEntityFields fieldsInResultset = EntityFieldsFactory.CreateTypedViewEntityFieldsObject(TypedViewType.MyTypedView); )

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 20-Apr-2004 21:47:38   

Otis wrote:

You should call GetFieldsInfo() simple_smile

(Adapter. SelfServicing doesn't have this method, you should then use: IEntityFields fieldsInResultset = EntityFieldsFactory.CreateTypedViewEntityFieldsObject(TypedViewType.MyTypedView); )

I'm using SelfServicing (any reason to switch to adapter?) and I got your second example to work in as far as being able to use a string to look up a field, but I think I'm on the wrong track. I guess I should back up to what I'm actually trying to accomplish.

I basically am trying to setup sort's and predicates so I can pass in a string column name and a value (or sort operator). So instead of


SortClauseFactory.Create( MyTableFieldIndex.Id, SortOperator.Ascending )

I'd have something similar to


SortClauseFactory.Create( FindTable( "Id" ), SortOperator.Ascending )

or


SortClauseFactory.Create( MyTableEntity.Fields[ "Id" ], SortOperator.Ascending )

but I can't get it to compile since the only overloads to the predicate and sort methods use the actual fieldIndexes. Are there any alternatives?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 20-Apr-2004 22:23:31   

The sortclause factory in fact creates IEntityField objects using the passed in index. It then creates a SortClause object. SO you can just create a SortClause object when you have a field instance simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 20-Apr-2004 22:47:44   

Otis wrote:

The sortclause factory in fact creates IEntityField objects using the passed in index. It then creates a SortClause object. SO you can just create a SortClause object when you have a field instance simple_smile

Hah, I got it simple_smile I'm so used to SortClauseFactory.Create

The following works for self servicing


//Method Parameters
IsDescending = true;
SortField = Id;

IEntityFields fields = EntityFieldsFactory.CreateTypedViewEntityFieldsObject(TypedViewType.MyViewTypedView); 

ISortExpression sortBy = new SortExpression( new SortClause( fields[ SortField ], (IsDescending ? SortOperator.Descending: SortOperator.Ascending) ) );


Thanks!

free4all
User
Posts: 35
Joined: 12-Sep-2003
# Posted on: 21-Apr-2004 09:33:58   

If there anything similar to CreateTypedViewEntityFieldsObject for a TypedList?

I'd like to do the same thing but I'm using TypedList instead of TypedView.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 21-Apr-2004 11:17:51   

Selfservicing typed lists have a private method BuildResultset(), so at the moment this is not doable, unless you alter the template to make the method public or the generated code.

I'll add it to the to-do list to make this method public and also his friend BuildRelationSet().

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 21-Apr-2004 21:37:29   

Otis wrote:

The sortclause factory in fact creates IEntityField objects using the passed in index. It then creates a SortClause object. SO you can just create a SortClause object when you have a field instance simple_smile

I decided to try this in an adapter implementation. I can retrieve the IEntity2 object, but the SortClause constructor takes an IEntity object, so I'm stuck.

How would I acheive a dynamic sort expression (or predicate) built around a string column name like above for adapter?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 21-Apr-2004 22:14:56   

You have to use the second constructor of the SortClause class, which takes an IEntityFieldCore object (which is implemented by the Adapter entityField objects) and a fieldpersistenceinfo object, just set this to null, like the factory does too, and of course the sort operator.

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 29-Apr-2004 21:10:33   

Okay, now I'm stuck on how to create a predicate expression using the above techniques. I'm still using adapter and I'm trying to put together a Like expression (but I will use other expressions in the future). I don't know how to construct a predicate expresison without using PredicateFactory and that doesn't take IEntityField2 objects (unless I'm missing something)?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 29-Apr-2004 22:18:56   

The predicate factory creates FieldCompareValuePredicate, FieldCompareRangePredicate, FieldLikePredicate etc. instances. These classes all accept an IEntityField instance. The predicate factory create a new field instance for you. You can however create that instance yourself using the EntityFieldFactory, or use an existing field, for example when you already have an instance of an entity (which has fields simple_smile ).

Please examine the generated PredicateFactory code, it perhaps gives you more insights simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 30-Apr-2004 01:24:43   

Otis wrote:

The predicate factory creates FieldCompareValuePredicate, FieldCompareRangePredicate, FieldLikePredicate etc. instances. These classes all accept an IEntityField instance. The predicate factory create a new field instance for you. You can however create that instance yourself using the EntityFieldFactory, or use an existing field, for example when you already have an instance of an entity (which has fields simple_smile ).

Please examine the generated PredicateFactory code, it perhaps gives you more insights simple_smile

I think I understand (it works anyway). Here's the code I came up with (if anyone's interested)


DataTable dt = new DataTable();
string searchValue = "My search text";
MyTypedView view = new MyTypedView();
IEntityField2 field = view.GetFieldsInfo()[ searchField ];

RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add( new FieldLikePredicate(field, null, "%" + searchValue + "%") );

adapter.FetchTypedView( vew.FetFieldInfo(), dt, bucket, 0, null, false );

The FieldLikePredicate takes an IEntityFieldCore as a parameter, I'm guessing that that is compatible with both the IEntityField and IEntityField2?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 30-Apr-2004 10:18:55   

The FieldLikePredicate takes an IEntityFieldCore as a parameter, I'm guessing that that is compatible with both the IEntityField and IEntityField2?

Yes, both interfaces derive from that interface simple_smile

Frans Bouma | Lead developer LLBLGen Pro