Avatar of Starr Duskk
Starr Duskk
Flag for United States of America

asked on 

Linq : unable to cast object

I'm getting this error:

Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[VB$AnonymousType_1`2[System.Guid,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[Oop.Entities.aspnet_Roles]'.

From this query:
    Public Function GetAvailableRoles(ByVal menuId2 As Integer) As IEnumerable(Of aspnet_Roles)
        Try
            Dim query As IEnumerable(Of aspnet_Roles) = Nothing
                query = From role In oEntities.aspnet_Roles, menu In oEntities.sssMenuRole _
                                   Select RoleId = role.RoleId, RoleName = role.RoleName, _
                                          RoleIDMnu = menu.RoleId, MenuIDMnu = menu.menuId _
                                   Where MenuIDMnu = menuId2 And RoleId = RoleIDMnu _
                                   Select RoleId, RoleName

I've also tried as a datatable, with this:
 As DataTable
                Dim availableRoles = From role In oEntities.aspnet_Roles, menu In oEntities.sssMenuRole _
                                   Select RoleId = role.RoleId, RoleName = role.RoleName, _
                                          RoleIDMnu = menu.RoleId, MenuIDMnu = menu.menuId _
                                   Where menuId = menuId And RoleId = RoleIDMnu _
                                   Select RoleId, RoleName

with this error.
Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_1`2[System.Guid,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[Linq.sssMenu]'.

I will attach the queries to create the tables. the roles table is the one created by microsoft automatically when you create a set of membership tables.



USE [Oop4]
GO
 
/****** Object:  Table [dbo].[aspnet_Roles]    Script Date: 02/26/2009 10:23:22 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[aspnet_Roles](
	[ApplicationId] [uniqueidentifier] NOT NULL,
	[RoleId] [uniqueidentifier] NOT NULL,
	[RoleName] [nvarchar](256) NOT NULL,
	[LoweredRoleName] [nvarchar](256) NOT NULL,
	[Description] [nvarchar](256) NULL,
 CONSTRAINT [PK__aspnet_Roles__31EC6D26] PRIMARY KEY NONCLUSTERED 
(
	[RoleId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
ALTER TABLE [dbo].[aspnet_Roles]  WITH CHECK ADD  CONSTRAINT [FK__aspnet_Ro__Appli__32E0915F] FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[aspnet_Applications] ([ApplicationId])
GO
 
ALTER TABLE [dbo].[aspnet_Roles] CHECK CONSTRAINT [FK__aspnet_Ro__Appli__32E0915F]
GO
 
ALTER TABLE [dbo].[aspnet_Roles] ADD  CONSTRAINT [DF__aspnet_Ro__RoleI__33D4B598]  DEFAULT (newid()) FOR [RoleId]
GO
 
 
USE [Oop4]
GO
 
/****** Object:  Table [dbo].[sssMenuRole]    Script Date: 02/26/2009 10:27:01 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[sssMenuRole](
	[menuId] [int] NOT NULL,
	[RoleId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_sssMenuRole] PRIMARY KEY CLUSTERED 
(
	[menuId] ASC,
	[RoleId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Starr Duskk

8/22/2022 - Mon