linq 返回一个筛选的项目列表,其中ID位于另一个匹配guid数组的列表中

zbdgwd5y  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(139)

在得到一些帮助后,使用另一个项目列表过滤列表。通常我会在SQL中编写代码,但我需要尝试在. NET中使用它。我有一个角色列表,每个角色都有一组权限,每个用户也是一组角色的成员,这将是过滤器。我试图让我的头周围的各种帮助文件,但没有一个例子,在这种格式,我可以找到。

Partial Class TestUsers
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Roles.Role = New List(Of Role)
        Roles.Role.Add(New Role(New Guid("EAC49E8F-BF55-44FD-BE69-5BBF7B5F289A"), "Admin", 0))
        Roles.Role.Add(New Role(New Guid("A174A3ED-17F8-4450-9B25-3D2A21FCDDFF"), "User", 0))
        Roles.Role.Add(New Role(New Guid("5780C40C-111A-4B4D-AA20-80E944B6B67A"), "Anon", 0))
        Roles.Role.Add(New Role(New Guid("7E1DC89C-4B9C-49F8-9A37-0FA22A7C8116"), "HR", 0))

        Dim UserPermission As New List(Of Role)
        UserPermission.Add(Roles.GetRoleByID(New Guid("EAC49E8F-BF55-44FD-BE69-5BBF7B5F289A")))
        UserPermission.Add(Roles.GetRoleByID(New Guid("A174A3ED-17F8-4450-9B25-3D2A21FCDDFF")))

        Dim PagePermission = RolePermission.GetPermissionFromString("Admin:1,1,1;User:1,0,0;Data:1,0,0;")
        Dim FilteredList = PagePermission.Where(Function(i) UserPermission.Contains(i.Role)).ToList
        Response.Write(FilteredList(0).Role.RoleName)

        Response.Write(UserPermission.Count())
        Response.Write("<br>")
        Response.Write(PagePermission.Count())
        Response.Write("<br>")
        Response.Write(FilteredList.Count())
    End Sub
    Public Class RolePermission
        Public Role As Role
        Public Read As Boolean
        Public Edit As Boolean
        Public Admin As Boolean
        Public Sub New(Role As Role, Read As String, Edit As String, Admin As String)
            Me.Role = Role
            Me.Read = Read
            Me.Edit = Edit
            Me.Admin = Admin
        End Sub
        Public Shared Function GetPermissionFromString(Permission As String) As List(Of RolePermission)
            Dim u As New List(Of RolePermission)
            If Permission = "" Then Permission = "Anon:0,0,0;"
            Dim l As String() = Permission.TrimEnd(";").Split(";")
            For Each s In l
                If Not IsNothing(s.Split(":")) Then
                    u.Add(New RolePermission(Roles.GetRoleByName(DbStr(Permission.Split(":")(0))), s.Split(":")(1).Split(",")(0), s.Split(":")(1).Split(",")(1), s.Split(":")(1).Split(",")(2)))
                End If
            Next
            Return u
        End Function
    End Class
    Public Class Role
        Public RoleID As Guid
        Public RoleName As String
        Public SiteGroupID As Integer
        Public Sub New(RoleID As Guid, RoleName As String, SiteGroupID As Integer)
            Me.RoleID = RoleID
            Me.RoleName = RoleName
            Me.SiteGroupID = SiteGroupID
        End Sub
    End Class
    Public Class Roles
        Public Shared Role As List(Of Role)
        Public Shared Function GetRoleByID(RoleID As Guid) As Role
            Return Role.Find(Function(s) s.RoleID = RoleID)
        End Function
        Public Shared Function GetRoleByName(RoleName As String) As Role
            Return Role.Find(Function(s) s.RoleName = RoleName)
        End Function
    End Class
End Class

字符串
过滤列表返回3,应该是2。

Dim FilteredList = PagePermission.Where(Function(i) UserPermission.Contains(i.Role)).ToListResponse.Write(FilteredList(0).Role.RoleName)

bgibtngc

bgibtngc1#

RolePermission.GetPermissionFromString()中有一个bug。在你得到DbStr(Permission.Split(":")(0))的地方,它应该是DbStr(s.Split(":")(0))

相关问题