Checking for related records

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 22-Oct-2007 22:37:16   

Hi, (Using Ver 2.00/VB/Adapter/SQL 2K)

In the following, Div has a 1:m relationship with DivProg & Section based on the PK of DivCode. I've been using the following code to determine if a Div PK has any related records in DivProg or Section:

Dim arrRelations() As EntityField2 = {DivProgFields.DivCode, SectionFields.DivCode}
 If Utilities.AnyRelatedRecords(arrRelations, PK) = True Then

by passing an array of relations to:

    Public Shared Function AnyRelatedRecords(ByVal arrRelations As Array, ByVal aForeignKey As String) As Boolean
        Dim result As Boolean
        For Each eField As EntityField2 In arrRelations
            If GetRelatedRecords(eField, aForeignKey) > 0 Then
                result = True
                Exit For
            Else
                result = False
            End If
        Next eField
        Return result
    End Function

  1. If the PK was a compound key (eg DivCode+ProgCode), how would I specify this to pass the array relations?
Dim arrRelations() As EntityField2 = {DivProgFields.DivCode, SectionFields.DivCode}
  1. Is there a better way of doing it? eg Not quite sure if this is a count of related records:
        Dim division As New DivEntity(aPK)
        If division.DivProg.Count > 0 Or _
            division.Section.Count > 0 Then
            'Related records
        End If

Any takers simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Oct-2007 05:21:10   

Hi Markiemac,

The relation is Div (1)->(m) DivProg. So, if you prefetch DivProg collection, you can do this:



if (Div.DivProg.Count > 0)
{
    ...
}
David Elizondo | LLBLGen Support Team