LINQ:其中列= application.user.name [英] LINQ: where column = application.user.name

查看:102
本文介绍了LINQ:其中列= application.user.name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

为了实现行级安全性,我在实体上插入了过滤条件.

In order to implement row level security, I am inserting filter condition on my entities.

该数据库包含一个Employee表,其中有我的雇员登录名(与user.name相同)和一个CIE_CODE.要过滤的所有表上都存在CIE_CODE列.

The database contains an Employee table in which I have the employee login (same as the user.name) and a CIE_CODE.The CIE_CODE column exists on all tables to be filtered.

过滤器逻辑应为:

如果当前用户为SecurityAdministrator权限,则查看所有记录;如果看不到,则仅查看CIE_CODE与当前用户的值匹配的记录.

If current user as permission SecurityAdministrator, then see all records, if not see only records where CIE_CODE matches the value from the current user.

我认为我首先需要查询当前员工记录以获取CIE_CODE,但是我的LINQ查询无法运行. (不受支持的表达式):

I figured that I first need to query tyhe current employee record to get the CIE_CODE, but my LINQ query does not run. (unsupported expression):

        Private Sub Employees_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Employee, Boolean)))

            If Application.User.HasPermission(SecurityAdministration) Then
                'ok to see all data
                filter = Function(e) True
            Else
                'filter on same company code.
                Dim CurEmp = (From e In Employees _
                              Where e.EmployeeLogin = Application.User.Name _
                              Select e).SingleOrDefault

                If CurEmp IsNot Nothing Then
                    filter = Function(e) e.Company.CIE_CODE = CurEmp.Company.CIE_CODE
                Else
                    filter = Function(e) False
                End If

            End If
        End Sub

推荐答案

将CurEmp.Company.CIE_CODE移至过滤器函数&你应该是对的.

Move CurEmp.Company.CIE_CODE to a variable outside the filter function & you should be right.

Private Sub Employees_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Employee, Boolean)))
    If Application.User.HasPermission(SecurityAdministration) Then
        'ok to see all data
        filter = Function(e) True
    Else
        'filter on same company code.
        Dim CurEmp = (From e In Employees _
            Where (e.EmployeeLogin = Application.User.Name) _
            Select e).SingleOrDefault

        If (CurEmp IsNot Nothing) AndAlso (CurEmp.Company IsNot Nothing) Then
            Dim code = CurEmp.Company.CIE_CODE            

            filter = Function(e) e.Company.CIE_CODE = code
        Else
            filter = Function(e) False
        End If
    End If
End Sub


您会注意到,我还为CurEmp.Company添加了第二个空检查.即使您知道它永远不会为null(无),还是一个好习惯,始终在引用实体的属性之一之前检查其是否为null.


You'll notice that I also added a second null check for CurEmp.Company. Even if you know this will never be null (Nothing), it's good practice to always check an entity for null, before referencing one of its properties.


这篇关于LINQ:其中列= application.user.name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆