Custom property: age from birthdate

Posts   
 
    
KingArt
User
Posts: 17
Joined: 09-Oct-2006
# Posted on: 02-Nov-2006 10:33:06   

In my database I have a datetime column that represents someone's birthdate. Now I can easily calculate that persons age with a function that I wrote, but I was wondering if it is possible to add a custom property 'Age' to my entity in LLBLGen that will use my function .

The reason I need this is that I want to make a predicate like so:

predicate.Add(KandidaatFields.Age >= leeftijdVan);

but I don't know how to make the 'Age'-field available here.

My first idea was to make a TypeConverter that converts a DateTime field to a number of years (the age of the person), but I also need to preserve the original birthdate field with a datetime. The conversion would work only one way, so I guess this isn't a very good idea anyway.

Could you please give me some advice on the best way to go here?

KingArt
User
Posts: 17
Joined: 09-Oct-2006
# Posted on: 02-Nov-2006 10:52:32   

I seem to have solved my immediate problem as follows:


predicate.Add(KandidaatFields.Geboortedatum <= DateTime.Today.AddYears(-1*leeftijdVan));

But I'm still curious if the solution I asked for earlier is possible, as it makes the predicate more readable.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Nov-2006 14:52:13   

To filter on the age like you have shown:

predicate.Add(KandidaatFields.Age >= leeftijdVan);

Needs the Age field to be cearted in the DB. table, so maybe by using a view with the Age field implemented by using DateDiff() & GetDate().

Your proposed solution is the best approach:

predicate.Add(KandidaatFields.Geboortedatum <= DateTime.Today.AddYears(-1*leeftijdVan));

There is no need to persist the age. As it is a variable of time. The DateOfBirth is more than enough to be persisted. And the age can always be calculated.

You can also use a Database function to calculate the Age. (please refer to the LLBLGen Pro manual: "Using the generated code -> Calling a database function")