我最近将VS 2005升级到2010年,对LinQ来说还算新。也许有人可以让我以正确的方式。
背景: 我有一个类型化的数据集,并使用Table AccessRule扩展了标准的SQLMembershipProvider。因此角色可以拥有无限的AccessRules(例如,“Administrator”具有“DeleteCustomer”)。我使用自定义成员资格提供程序继承自SqlMemberShipProvider并具有重载函数hasAccess(一个具有内存数据集作为参数,另一个直接使用数据库)。
这是完整的模型:
现在我需要知道f.e.如果UserID = '89f9ea8d-8ae1-460b-a430-aa433261feec'
的用户具有AccessRule“DeleteCustomer”。
在SQL中它将是这样的:
SELECT CASE WHEN aspnet_AccessRule.idAccessRule IS NULL THEN 0 ELSE 1 END AS Access
FROM aspnet_RoleAccessRule INNER JOIN
aspnet_AccessRule ON aspnet_RoleAccessRule.fiAccessRule = aspnet_AccessRule.idAccessRule INNER JOIN
aspnet_Roles ON aspnet_RoleAccessRule.fiRole = aspnet_Roles.RoleId INNER JOIN
aspnet_UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId
WHERE (aspnet_UsersInRoles.UserId = @UserID) AND (aspnet_AccessRule.RuleName =@RuleName)
简而言之:
如何使用LinQ从aspnet_UsersInRoles
到aspnet_AccessRule
?
提前谢谢你......
修改:
虽然也欢迎C#,但我更喜欢VB.Net。
这就是我所拥有的,但它不起作用。
Dim query = From accRule In dsAuth.aspnet_AccessRule _
From roleAccRule In dsAuth.aspnet_RoleAccessRule _
From role In dsAuth.aspnet_Roles _
From userRole In dsAuth.aspnet_UsersInRoles _
Where roleAccRule.fiAccessRule = accRule.idAccessRule _
And roleAccRule.fiRole = role.RoleId _
And userRole.RoleId = role.RoleId _
And userRole.UserId = userID And accRule.RuleName = accessRule
Select accRule.idAccessRule
Return query.Any
我收到"Definition of method SelectMany is not accessible in this context"
编译器警告,第二个From
突出显示。
我假设它与aspnet_RoleAccessRule和aspnet_UsersInRoles中的复合键有关。有什么建议吗?
这在第一个逗号上给出了相同的例外:
Dim query = From accRule In dsAuth.aspnet_AccessRule, _
roleAccRule In dsAuth.aspnet_RoleAccessRule, _
role In dsAuth.aspnet_Roles, _
userRole In dsAuth.aspnet_UsersInRoles _
Where accRule.idAccessRule = roleAccRule.fiAccessRule _
And roleAccRule.fiRole = role.RoleId _
And userRole.RoleId = role.RoleId _
And userRole.UserId = userID And accRule.RuleName = accessRule
Select accRule.idAccessRule
Return query.Any
这是连接语法但有类似错误(Join
在上下文中无法访问..):
Dim userID As Guid = DirectCast(Membership.GetUser.ProviderUserKey, Guid)
Dim query = From accRule In dsAuth.aspnet_AccessRule _
Join roleAccRule In dsAuth.aspnet_RoleAccessRule _
On accRule.idAccessRule Equals roleAccRule.fiAccessRule _
Join role In dsAuth.aspnet_Roles _
On role.RoleId Equals roleAccRule.fiRole _
Join userRole In dsAuth.aspnet_UsersInRoles _
On userRole.RoleId Equals role.RoleId _
Where userRole.UserId = userID And accRule.RuleName = accessRule
Select accRule.idAccessRule
Return query.Any
解决方案:我忘记导入命名空间System.LinQ
。所有这些查询都有效。
旁注:Join-Syntax是迄今为止最快的查询。看看我的后续问题:Why is LINQ JOIN so much faster than linking with WHERE?
答案 0 :(得分:3)
我担心会有人来开始向我扔石头,但我会抓住机会。我可能会从这开始:
var AccessRules = from ar in aspnet_AccessRule
from rar in aspnet_Role
from r in aspnet_Roles
from uir in aspnet_UsersInRoles
where ar.idaccessrule == rar.fiAccessRule
where rar.fiRole == r.RoleId
where r.RoleId == uir.RoleId
select ar;
嗯..我实际上不知道你的数据集看起来如何,但我认为你从这个伪代码中得到了重点。
修改:以下是可能对您有所帮助的工具链接:Linqer
答案 1 :(得分:1)
此线程可能会帮助您将SQL转换为LINQ: