如何连接 LinQ to(类型化)数据集?

发布于 2024-10-29 09:37:35 字数 3664 浏览 3 评论 0原文

我最近将 VS 2005 升级到 2010,并且对 LinQ 还很陌生。也许有人可以让我走上正确的道路。

背景: 我有一个类型化数据集,并使用表 AccessRule 扩展了标准 SQLMembershipProvider。因此,一个角色可以有无限个访问规则(例如“管理员”有“删除客户”)。我使用一个继承自 SqlMemberShipProvider 的自定义成员资格提供程序,并具有重载函数 hasAccess(一个以内存数据集作为参数,另一个直接使用数据库)。

这是完整的模型:

在此处输入图像描述

现在我需要知道是否有 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_UsersInRolesaspnet_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

我收到“在此上下文中无法访问方法 SelectMany 的定义” 编译器警告,并且第二个 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 是迄今为止最快的查询。看看我的后续问题: 为什么LINQ JOIN 比用 WHERE 链接快得多吗?

i recently upgraded VS 2005 to 2010 and am fairly new to LinQ. Maybe somebody can put me in the right way.

Background:
I have a typed dataset and have the standard SQLMembershipProvider extended with a Table AccessRule. So a role can have infinitely AccessRules(f.e. "Administrator" has "DeleteCustomer"). I use a custom membership provider that inherits from SqlMemberShipProvider and has an overloaded function hasAccess(one with a memory-dataset as parameter and the other uses the database directly).

This is the complete Model:

enter image description here

Now i need to know f.e. if a User with UserID='89f9ea8d-8ae1-460b-a430-aa433261feec' has AccessRule "DeleteCustomer".

In SQL it would be something like this:

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) 

In short:

how do i get from aspnet_UsersInRoles toaspnet_AccessRule with LinQ?

Thank you in advance...

Edit:

Although C# is also welcome, i'm preferring VB.Net.

This is what i have, but it doesn't work.

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

I get a "Definition of method SelectMany is not accessible in this context" compiler warning and the secondFrom is highlighted.
I assume it has something to do with the composite-keys in aspnet_RoleAccessRule and aspnet_UsersInRoles. Any suggestions?

This gives the same exception on the first comma:

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

This is the join syntax but with similar error(Join is not accessible in context..):

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

Solution: I've forgotten to import the Namespace System.LinQ. All these queries are working.

Side note: the Join-Syntax is by far the fastest query. Have a look at my followup-question: Why is LINQ JOIN so much faster than linking with WHERE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-11-05 09:37:35

我担心有人会过来向我扔石头,但我会抓住机会。我可能会这样开始:

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

I am afraid that someone will come and start throwing rocks at me but I will take my chances. I would probably start with this:

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;

Hmmm.. I actuall don't know how your dataset looks, but I think you get the point from this pseudocode.

Edit: here is the link to tool that might help you: Linqer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文