In trying to retrieve info from my database using linq but it won't work. When i excecute the code it throws the exception "'Unable to cast the type 'System.Guid' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.'

Public Class HomeTree
    Inherits ApplicationFolders

    Private _CanList As Boolean
    Public Property CanList() As Boolean
        Get
            Return _CanList
        End Get
        Set(value As Boolean)
            _CanList = value
        End Set
    End Property

    Private _CanEdit As Boolean
    Public Property CanEdit() As Boolean
        Get
            Return _CanEdit
        End Get
        Set(value As Boolean)
            _CanEdit = value
        End Set
    End Property

    Private _CanAdd As Boolean
    Public Property CanAdd() As Boolean
        Get
            Return _CanAdd
        End Get
        Set(value As Boolean)
            _CanAdd = value
        End Set
    End Property

    Private _CanDelete As Boolean
    Public Property CanDelete() As Boolean
        Get
            Return _CanDelete
        End Get
        Set(value As Boolean)
            _CanDelete = value
        End Set
    End Property
    Public Sub New()

    End Sub
End Class

Retrieving Data

Dim GBItems As List(Of HomeTree) = (From t In db.ApplicationFolders
                                            Join r In db.AppFolderRoleConfig On {t.AppFolderId, isLoggedInUser.ID_Role} Equals {r.AppFolderID, r.RoleID}
                                            Where t.ParentID = 0 AndAlso t.TypeID = 1 Select New HomeTree With {
                                                                                          .ID_Key = t.ID_Key,
                                                                                          .AppFolderId = t.AppFolderId,
                                                                                          .Name = t.Name,
                                                                                          .Active = t.Active,
                                                                                          .Description = t.Description,
                                                                                          .ParentID = t.ParentID,
                                                                                          .TypeID = t.TypeID,
                                                                                          .CanAdd = r.CanAdd,
                                                                                          .CanDelete = r.CanDelete,
                                                                                          .CanEdit = r.CanEdit,
                                                                                          .CanList = r.CanList
                                                                                          }).ToList()

Ive tried googling for my issue but i cannot figure it out.

Image to show what happens if i try answer given.

Error Msg.

1

There are 1 best solutions below

9
Power Mouse On

try this query example. since i dont know your db structure (was not provided) and data example i assumed on my own. i think this is what are you looking for

New With {Key.a = af.AppfolderiD.Value, Key.v = isLoggedInUser_ID_Role}

code

    Dim isLoggedInUser_ID_Role As Integer = 1

    Dim GBItems = (
        From af In ApplicationFolders
        Join ar In AppFolderRoleConfig On
            New With {Key.a = af.AppfolderiD.Value, Key.v = isLoggedInUser_ID_Role} Equals 
            New With {Key.a = ar.AppFolderID.Value, Key.v = ar.RoleID.Value} 
    Where af.ParentID = 0 AndAlso af.TypeID = 1 
    Select New HomeTree() With {
        .ID_Key = af.ID_Key,
        .AppFolderId = af.AppfolderiD,
        .Name = af.Name '...
    })
    GBItems.Dump()

add image to prove that code is works

enter image description here