Dynamic sorting

Posts   
 
    
Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 02-Nov-2004 12:43:29   

Hi!

I find myself writing many and huge switch statements to get around issues which I hope will have a more dynamic capability (only I haven't found it out yet.)

There is a great collection in the EntityCollections called Fields. It is obviously for dynamic access to fields in the EntityClass.


private string testFunc(string sFieldName)
{
    SomeTableCollection sometablecollection = new SomeTableCollection();
    sometablecollection.GetMulti(null);
    return sometablecollection.Fields[sFieldName].ToString();
}

This is great!

I now wonder if there is a similar appoach to the FieldIndex. I would like to be able to pass which field to sort on to a function. So far I have handled this by using Switch statements based on the sSortField argument. But that is a very cumbersome way to do it. Especially with many tables and Sort field candidates. I wonder if there is something like:


private string testFunc(string sSortField)
{
    SomeTableCollection sometablecollection = new SomeTableCollection();
    ISortExpression sorter = new SortExpression();
    sorter.Add(SortClauseFactory.Create(SomeTableFieldIndex[sSortField], sortOperator.Ascending));
    sometablecollection.GetMulti(null);
}

Is there some similar way to do this already?

Gratefully,

!Rob

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 02-Nov-2004 13:42:12   

Rob wrote:

Hi!

I find myself writing many and huge switch statements to get around issues which I hope will have a more dynamic capability (only I haven't found it out yet.)

There is a great collection in the EntityCollections called Fields. It is obviously for dynamic access to fields in the EntityClass.


private string testFunc(string sFieldName)
{
    SomeTableCollection sometablecollection = new SomeTableCollection();
    sometablecollection.GetMulti(null);
    return sometablecollection.Fields[sFieldName].ToString();
}

This is great!

Err simple_smile I hope I won't spoil your party, but I'm not aware of a Fields collection in a collection. The entities inside a collection however have a fields collection. So you probably meant: return sometablecollection[index].Fields[sFieldName].ToString(); ?

I now wonder if there is a similar appoach to the FieldIndex. I would like to be able to pass which field to sort on to a function. So far I have handled this by using Switch statements based on the sSortField argument. But that is a very cumbersome way to do it. Especially with many tables and Sort field candidates. I wonder if there is something like:


private string testFunc(string sSortField)
{
    SomeTableCollection sometablecollection = new SomeTableCollection();
    ISortExpression sorter = new SortExpression();
    sorter.Add(SortClauseFactory.Create(SomeTableFieldIndex[sSortField], sortOperator.Ascending));
    sometablecollection.GetMulti(null);
}

Is there some similar way to do this already?

Of course simple_smile


private string testFunc(SomeTableFieldIndex index)
{
    SomeTableCollection sometablecollection = new SomeTableCollection();
    ISortExpression sorter = new SortExpression();
    sorter.Add(new SortClause(EntityFieldFactory.Create(index), sortOperator.Ascending));
    sometablecollection.GetMulti(null, ..... , sorter, ....);
}

This should do it simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 03-Nov-2004 01:44:30   

Hey Otis!

In regards to .Fields collection, yes of course you are right, I saw my error after I posted it.

I realise that your solution for my sorting problem would work. However it is not quite what I am after. I want to be able to pass in a string containing the name of the field to sort on, not its' LLBLGen generated index. What I am doing in this case is, I am rendering dropdownlists onto forms, and the information about what goes in the dropdownlists is stored in the database. It is sort of a Content Management System approach to building forms. This is so that my application can have configurable data entry forms.

So, can I pass in a string with the field name and use this string to control the sorting?

!R

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-Nov-2004 10:13:43   

Rob wrote:

Hey Otis!

In regards to .Fields collection, yes of course you are right, I saw my error after I posted it.

I realise that your solution for my sorting problem would work. However it is not quite what I am after. I want to be able to pass in a string containing the name of the field to sort on, not its' LLBLGen generated index. What I am doing in this case is, I am rendering dropdownlists onto forms, and the information about what goes in the dropdownlists is stored in the database. It is sort of a Content Management System approach to building forms. This is so that my application can have configurable data entry forms. So, can I pass in a string with the field name and use this string to control the sorting? !R

If you know the type of the entity, sure simple_smile . do:

IEntityFields fields = EntityFieldsFactory.Create(EntityType.SomeTableEntity); and then instead of calling EntityFieldFactory.Create(enum), you simply pass: fields["name"] to the constructor of SortClause() simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 04-Nov-2004 03:36:49   

Hey Otis!

I guess you mean: wink


IEntityFields fields = EntityFieldsFactory.CreateEntityFieldsObject(EntityType.SomeTableEntity);

Thanks for the tip, it worked like a charm. I wish there was a way I could find these things out for myself.

!Rob

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Nov-2004 09:03:18   

Rob wrote:

Hey Otis!

I guess you mean: wink


IEntityFields fields = EntityFieldsFactory.CreateEntityFieldsObject(EntityType.SomeTableEntity);

err, yes wink I didn't look closely to the method.

Thanks for the tip, it worked like a charm. I wish there was a way I could find these things out for myself. !Rob

There is no other way than to look into the generated code and into the reference manual, where these classes/methods are explained. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
un.real
User
Posts: 12
Joined: 27-May-2005
# Posted on: 19-Jul-2005 11:24:27   

hello guys;

I've read your discussion but couldn't solve my problem. I think my problem is the same with Rob's: sorting a typedList by a string parameter.

I want to do something like this:


private sometableTypedList GridListFiller(string sortField){
   sometableTypedList Tlist = new sometableTypedList();

   ISortExpression sorter = new SortExpression();
   sorter.Add(sortField, SortOperator.Descending);

   Tlist.Fill(0,sorter,false,null);

   return Tlist;

}

I tried the piece of code above (the one Otis wrote) but it didn't work.??

please help me simple_smile thanks in advance. abdullah.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 20-Jul-2005 11:22:30   

what does 'didn't work' mean exactly? You got an exception, error or nothing was sorted? Could you please turn on tracing for the DQE (See 'Using the generated code / troubleshooting and debugging' in the docs) to see which query is generated exactly?

Frans Bouma | Lead developer LLBLGen Pro
un.real
User
Posts: 12
Joined: 27-May-2005
# Posted on: 20-Jul-2005 17:59:34   

I started a new thread for this and just saw that u replied to it. wink

BTW, by "didn't work", I meant it didn't sort the data. probably it was me making a mistake.

jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 02-Jan-2007 17:41:42   

I am using an LLBLGenProDataSource with a DataContainerType or TypedView. This binds to a datagrid.

I am trying to construct some dynamic sorting. In a post in this thread, Otis posted the following code snippit:

ISortExpression sorter = new SortExpression(); sorter.Add(new SortClause(EntityFieldFactory.Create(index), sortOperator.Ascending));

How would you deal with the sorting in this case, as the SortClause seems to need an EntityField, but the LLBLGenProDataSource is using a TypedView?

Thanks for any help

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 03-Jan-2007 03:08:59   

An overload of create will accept the typed view as the first parameter and the field name as the second to create the field.

EntityFieldFactory.Create("myTypedView", "fieldName")

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-Jan-2007 09:59:33   

A typedview also uses Entityfield(2) objects simple_smile

Frans Bouma | Lead developer LLBLGen Pro
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 03-Jan-2007 10:11:58   

Thanks - that works great now simple_smile