Starr Duskk
asked on
Linq : unable to cast object
I'm getting this error:
Unable to cast object of type 'System.Data.Objects.Objec tQuery`1[V B$Anonymou sType_1`2[ System.Gui d,System.S tring]]' to type 'System.Collections.Generi c.IEnumera ble`1[Oop. Entities.a spnet_Role s]'.
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.DataQuer y`1[VB$Ano nymousType _1`2[Syste m.Guid,Sys tem.String ]]' to type 'System.Collections.Generi c.IEnumera ble`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.
Unable to cast object of type 'System.Data.Objects.Objec
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.DataQuer
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
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Post the code as you implemented it with all variables definitions that are used as well.
Your code returns a query and is not enumerated yet. Return a query.ToArray() or query.ToList()
I must admit never having done Linq in VB.NET, only in C#,
but can't you do something similar to:
return oEntities.aspnet_Roles.Whe re(r => r.sssMenuRole.menuID == menuId2).ToArray();
(don't know VB.NET equivalent)
I also assume that your linq model has relationships between asp_role and menu table (based on the role_id)
I must admit never having done Linq in VB.NET, only in C#,
but can't you do something similar to:
return oEntities.aspnet_Roles.Whe
(don't know VB.NET equivalent)
I also assume that your linq model has relationships between asp_role and menu table (based on the role_id)
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I tried that:
Dim query As New List(Of aspnet_Roles)
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 role).ToList()
the "select role" errors with same error, 'either not declared or not in current scope.'
If I do it this way:
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).ToList()
with this change:
Select RoleId, RoleName
it errors with generic list of anonymous type cannot be converted to generic list of aspnet_Roles.
sigh.
Dim query As New List(Of aspnet_Roles)
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 role).ToList()
the "select role" errors with same error, 'either not declared or not in current scope.'
If I do it this way:
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).ToList()
with this change:
Select RoleId, RoleName
it errors with generic list of anonymous type cannot be converted to generic list of aspnet_Roles.
sigh.
what's the first select used for anyway?
query = (From role In oEntities.aspnet_Roles, menu In oEntities.sssMenuRole _
Where MenuIDMnu = menuId2 And RoleId = RoleIDMnu _
Select role).ToList()
ASKER
Here is the query built-by-hand:
Dim sql As New StringBuilder()
sql.AppendLine("SELECT r.RoleId, r.RoleName ")
sql.AppendLine("FROM dbo.aspnet_Roles r ")
sql.AppendLine("WHERE r.RoleId IN ")
sql.AppendLine(" (select roleID from dbo.sssMenuRole where menuId = @MenuId) ")
sql.AppendLine("Order By r.RoleName")
Dim sql As New StringBuilder()
sql.AppendLine("SELECT r.RoleId, r.RoleName ")
sql.AppendLine("FROM dbo.aspnet_Roles r ")
sql.AppendLine("WHERE r.RoleId IN ")
sql.AppendLine(" (select roleID from dbo.sssMenuRole where menuId = @MenuId) ")
sql.AppendLine("Order By r.RoleName")
I'm getting confused, you're selecting roleID + roleName, but expecting to get out a complete role.
select the role as a complete object using linq, or create a new class that contains only roleID, roleName and return that if you like.
but selecting just 2 properties will not working, unless you explicitly create a new object and set these properties yourself, but I don't see the sense of doing that.
select the role as a complete object using linq, or create a new class that contains only roleID, roleName and return that if you like.
but selecting just 2 properties will not working, unless you explicitly create a new object and set these properties yourself, but I don't see the sense of doing that.
ASKER
If you refer to the previous posts, you'll see I've tried to select all the fields...
>> Select role).ToList()
>>the "select role" errors with same error, 'either not declared or not in current scope.'
But it errors.
I don't care how I do it, as long as I can get it to work.
Can you tell me how to reproduce that query in linq? with selecting *
>> Select role).ToList()
>>the "select role" errors with same error, 'either not declared or not in current scope.'
But it errors.
I don't care how I do it, as long as I can get it to work.
Can you tell me how to reproduce that query in linq? with selecting *
return oEntities.aspnet_Roles.ToL ist()
that's all
that's all
ASKER
return? can you spell it out for me please?
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 role).ToList()
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 role).ToList()
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
It looks like it's working.
Thanks!
I have another question using this same query, if you can look at it:
http://www.experts-exchang e.com/Prog ramming/La nguages/.N ET/LINQ/Q_ 24204214.h tml
thanks!
Thanks!
I have another question using this same query, if you can look at it:
http://www.experts-exchang
thanks!
ASKER
thanks!
ASKER
"role is not declared or is not in the current scope"