Multiple WithPaths

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 03-Feb-2009 16:59:38   

I have the following code to create multiple WithPaths


    Public Enum PrefetchItemEnum
        None
        Attendance
        StudActive
        StudData
    End Enum

    Public Shared Function FetchEntity(ByVal studentId As String, ByVal prefetchItem1 As PrefetchItemEnum, ByVal prefetchItem2 As PrefetchItemEnum, ByVal prefetchItem3 As PrefetchItemEnum) As StudentMstrEntity

        Dim meta As New Linq.LinqMetaData(New DataAccessAdapter)

        Dim q = From d In meta.StudentMstr Where d.StudentId = studentId Select d

        If prefetchItem1 = PrefetchItemEnum.Attendance Then
            q = q.WithPath(New PathEdge(Of AttendanceEntity)(StudentMstrEntity.PrefetchPathAttendance))
        End If

        If prefetchItem2 = PrefetchItemEnum.StudActive Then
            q = q.WithPath(New PathEdge(Of StudActiveEntity)(StudentMstrEntity.PrefetchPathStudActive))
        End If

        If prefetchItem3 = PrefetchItemEnum.StudData Then
            q = q.WithPath(New PathEdge(Of StudDataEntity)(StudentMstrEntity.PrefetchPathStudData))
        End If

        Dim c = q.ToEntityCollection(Of StudentMstrEntity)()

        If c.Count >= 0 Then Return c(0)

        Return Nothing
    End Function

The .ToEntityCollection is an extension that Gabe Nodland(sp?) wrote:

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToEntityCollection(Of t As EntityBase2)(ByVal q As IQueryable) As EntityCollection(Of t)
        If Not TypeOf q Is ILLBLGenProQuery Then
            Throw New ApplicationException("Only ILLBLGenProQuery may be outputed as EntityCollections")
        End If

        Return CType(q, ILLBLGenProQuery).Execute(Of EntityCollection(Of t))()
    End Function

But, it seems like it only does one of the WithPaths not the ones selected.

Am I doing the WithPath logic correctly?

Thanks

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 03-Feb-2009 17:38:47   

Okay, I resolved it on my own simple_smile , this code works:

    Public Enum PrefetchItemEnum
        None
        Attendance
        StudActive
        StudData
    End Enum

    Public Shared Function FetchEntity(ByVal studentId As String, ByVal ParamArray prefetchItem() As PrefetchItemEnum) As StudentMstrEntity

        Dim meta As New Linq.LinqMetaData(New DataAccessAdapter)

        Dim q = From d In meta.StudentMstr Where d.StudentId = studentId Select d

        Dim wp As New List(Of IPathEdge)

        For Each pa In prefetchItem
            If pa = PrefetchItemEnum.Attendance Then wp.Add(New PathEdge(Of AttendanceEntity)(StudentMstrEntity.PrefetchPathAttendance))
            If pa = PrefetchItemEnum.StudActive Then wp.Add(New PathEdge(Of StudActiveEntity)(StudentMstrEntity.PrefetchPathStudActive))
            If pa = PrefetchItemEnum.StudData Then wp.Add(New PathEdge(Of StudDataEntity)(StudentMstrEntity.PrefetchPathStudData))
        Next

        Dim c = q.WithPath(wp.ToArray).ToEntityCollection(Of StudentMstrEntity)()

        If c.Count >= 0 Then Return c(0)

        Return Nothing
    End Function