GetScalar() on generic collection

Posts   
 
    
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 03-Nov-2009 12:55:56   

Hi,

all my table have an integer field called "Version" and I need the maximum of the current values.

My goal is to have a general function that executes an aggregate function on a collection of which I just know the name.

This is what I have done so far:

//Sample calls
int maxCustomers = GetMaxVersion("Customers");
int maxProducts = GetMaxVersion("Products");

public int GetMaxVersion(String entityName)
{
   int result = 0;

   entityName += "Entity";

   //Find out corresponding entity type
   EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);

   //Create a new collection instance based on typeOfEntity
   IEntityCollection myCollection = FactoryClasses.GeneralEntityCollectionFactory.Create(typeOfEntity);

   //I HAVE NO IDEA HOW TO COMPLETE THE FOLLOWING LINE
   int result = (int)???.GetScalar(???FieldIndex.Version, SD.LLBLGen.Pro.ORMSupportClasses.AggregateFunction.Max);

return result;
}

My problem is the last statement in the GetMaxVersion method. I have no idea how to write generic code that does the job.

Could anybody please advice me? Thanks so much!

I'm using LLBL 2.6 and SelfService (and I CAN NOT switch to Adapter anymore ;-)

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 03-Nov-2009 20:35:35   

You can create fields using the EntityFieldsFactory, in the same way that you have created the EntityCollection.

Or you could create an instance of the entity using the EntityFactory and then use

entity.Fields["Version"]

Matt

softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 04-Nov-2009 06:53:34   

Hello Matt,

but what about

(int) ???.GetScalar(...

I can't just write

(int) myCollection.GetScalar(...

because this won't compile. Intellisense does not even offer me the GetScalar() method. I guess I have to do a cast. However, I don't know how.

Thanks for your help!!!

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 04-Nov-2009 10:31:05   

I think another approach might be much easier.

Since this is dynamic, you might need to try using a DynamicList. With one field, AggregateFunction.Max specified, and a MaxAmountOfItemsToReturn set to 1 for TOP 1.