It errors:
"role is not declared or is not in the current scope"
I'm getting this error:
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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
(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 was encountering the same issue. My VB.net 9.0 / Entity Framework approach is in the code snippet below
Note that if you are using Entity Framework for a database that includes related tables, HeaderTable and DetailTable. The foreign keys in DetailTable to HeaderTable are not generated by Entity Framework.
So the best way to consider the LINQ query where related tables are involved is that your iterator (c in my code snippet) works across all columns in your related tables. c is an iterator across each composite row from the inner join of HeaderTable -= DetailTable.
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.
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")
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.
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 *
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()
It looks like it's working.
Thanks!
I have another question using this same query, if you can look at it:
http://www.experts-exch
than
Business Accounts
Answer for Membership
by: FernandoSotoPosted on 2009-02-26 at 08:47:07ID: 23746619
Hi BobCSD;
In both cases the return type of the queries are NOT of type aspnet_Roles but of Anonymous type. This is because you do not select the whole record of aspnet_Roles in the select but only two fields.
Select RoleId, RoleName '<=== Returns Anonymous type
In order for the select to return a type of aspnet_Roles you would need a select like this:
Select role
But because the way the query is set up I believe that the variable role is out of scope at that point. Give it a try anyway.
Fernando