Custom method to count records from a related entitycollection via generated code

Posts   
 
    
SanderF
User
Posts: 125
Joined: 11-Dec-2006
# Posted on: 07-Sep-2021 15:09:06   

I'm used LLBLGen v5.8 with SelfServicing and the LLBLGen Pro Runtime Framework . We have some performance issues in our application because we need to know in a lot of places how many records are in a related entitycollection. An example: we have a entity Aanhef (Salutation) with a related 1:n entitycollection Personen (Persons). Now we use the following code:

return aanhef.Personen.Count;

In this situation all the person-records are fetched which are linked to that salutation-record. We don't need the collection itself, we only need to know how much records there are. I know that i need to use the GetDbCount() method on the AanhefCollection with a filter, which counts only the records via a quick SQL-server call. To make it easer for our developers i want to add a method to all related entities via the generated code.

So i added the following code to entityInclude.template:

[Browsable(false)]
public int Get<[MappedFieldNameRelation]>Count()
{
    return new <[RelatedEntityName]>Collection().GetDbCount(<[RelatedEntityName]>Fields.<[RelationFieldName]> == ID);
}

which produces the following generated code:

[Browsable(false)]
public int GetPersonenCount()
{
     return new PersoonCollection().GetDbCount(PersoonFields. == ID);
}

My only problem is the placeholder <[RelationFieldName]> won't work (it results in an empy string). I've searched in https://www.llblgen.com/Documentation/5.8/SDK/templates_tdl.htm to find a token with contains the value of the field 'Fk field name' from the Normal Relationship Editor, but didn't find it. Can you please help me? Is there maybe a standard solution for this?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 07-Sep-2021 19:35:09   

Do you need to add this method to all generated entities, for all their related collections? And in this case, is it safe to assume all PK fields or properties mapped to the PK would be a single property (i.e. no composite PKs), and it's called ID?

If you only need this for one entity, then you can add it manually to a user code region inside the generated class, or better in a partial calls file.

If you need it for all entities, then using the templates is the way to go, and then you need to do loop on the RelationFIeld first, as follows:

<[Foreach RelationField]>

Do your code here...<[RelationFieldName]>

<[NextForeach]>

SanderF
User
Posts: 125
Joined: 11-Dec-2006
# Posted on: 08-Sep-2021 08:23:14   

Thanks Walaa,

I want this method for all related entities in all entities, so i want to use templates indeed. I've changed the <[RelationFieldName]> to <[RelatedEntityRelationFieldName]> and it works!

<[Foreach RelationField]>
[Browsable(false)]
public int Get<[MappedFieldNameRelation]>Count()
{
       return new <[RelatedEntityName]>Collection().GetDbCount(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]> == ID);
}<[NextForeach]>

generates:

[Browsable(false)]
public int GetPersonenCount()
{
     return new PersoonCollection().GetDbCount(PersoonFields.AanhefID == ID);
}