工作流中的复杂 LINQ 查询

发布于 2024-10-20 11:22:43 字数 1183 浏览 3 评论 0原文

我们有一个“文档”表。每份文件必须在生效前获得批准。

文件审批过程需要多个参与者。例如,设计师第一批准,执行经理第二批准,总经理第三批准。总经理不能先行批准。

该文档有一组批准者。 “批准者”具有“索引”、“批准者名称”和“已批准”属性。

问题是如何使用 LINQ 显示用户现在必须批准的文档列表。当特定用户批准文档时,它们应该出现在列表中。

到目前为止,我们一直在使用以下手动过滤代码:

   Public ReadOnly Property RequiresMyApproval() As Boolean

        Get


            If Me.Approved OrElse Me.Approvals.Count = 0 Then Return False

            Dim closestUnapproval = GetClosestUnapproval()

            Return (closestUnapproval IsNot Nothing AndAlso closestUnapproval.ParentUser.Oid = CurrentUser.Oid)

        End Get

    End Property


    Private Function GetClosestUnapproval() As DocumentApproval
        Dim closestUnapprovedIndex As Integer = -1
        Dim closestUnapproval As DocumentApproval = Nothing



        For Each approval In Me.Approvals
            If Not approval.Approved AndAlso approval.Index < closestUnapprovedIndex OrElse Not approval.Approved AndAlso closestUnapprovedIndex = -1 Then
                closestUnapprovedIndex = approval.Index
                closestUnapproval = approval
            End If
        Next


        Return closestUnapproval

    End Function

We have a "Documents" table. Each document must be approved before it turns active.

Document approval process requires multiple participants. For example, designer approves first, executive manager second, general manager third. General manager can't approve before executive.

The Document has a collection of Approvers. "Approvers" have "Index", "ApproverName" and "Approved" properties.

The question is how use LINQ to show the list of documents the user has to approve now. Documents should appear in the list when it's time for specific user to approve them.

So far we've been using the following manual filtering code:

   Public ReadOnly Property RequiresMyApproval() As Boolean

        Get


            If Me.Approved OrElse Me.Approvals.Count = 0 Then Return False

            Dim closestUnapproval = GetClosestUnapproval()

            Return (closestUnapproval IsNot Nothing AndAlso closestUnapproval.ParentUser.Oid = CurrentUser.Oid)

        End Get

    End Property


    Private Function GetClosestUnapproval() As DocumentApproval
        Dim closestUnapprovedIndex As Integer = -1
        Dim closestUnapproval As DocumentApproval = Nothing



        For Each approval In Me.Approvals
            If Not approval.Approved AndAlso approval.Index < closestUnapprovedIndex OrElse Not approval.Approved AndAlso closestUnapprovedIndex = -1 Then
                closestUnapprovedIndex = approval.Index
                closestUnapproval = approval
            End If
        Next


        Return closestUnapproval

    End Function

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

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

发布评论

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

评论(2

莫相离 2024-10-27 11:22:43

我建议您使用 LinqKit (http://www.albahari.com/nutshell/linqkit.aspx) 免费库,它有助于在 LINQ 中进行动态查询...您还可以使用 LinqPad 轻松测试查询,无需编写代码有什么事先...

I recommend you to use LinqKit (http://www.albahari.com/nutshell/linqkit.aspx) free library, it helps with dynamic queries in LINQ... You can also use LinqPad to test you queries easy without need to code anything first...

沉睡月亮 2024-10-27 11:22:43

如果我正确地理解了这个问题:

//The person who we're building this query around
Approver me;

Documents
    .Where(d => !d.Approvers.Where(a => a == me).First().Approved)
    .Where(d => 
        d.SelectMany(x =>
            x.Approvers
            .Where(a => a.Index < me.Index)
            .Where(a => !a.Approved))
    .Count() == 0)

它的作用是针对每个文档,它会在批准者列表中找到您,并检查您是否尚未批准该文档。如果您还没有批准,那么它会选择所有比您少的批准者,并获取尚未批准该文档的人数,如果该计数为零,则该文档通过。

If I understand the problem correctly:

//The person who we're building this query around
Approver me;

Documents
    .Where(d => !d.Approvers.Where(a => a == me).First().Approved)
    .Where(d => 
        d.SelectMany(x =>
            x.Approvers
            .Where(a => a.Index < me.Index)
            .Where(a => !a.Approved))
    .Count() == 0)

What this does is for each document, it finds you in the list of approvers and checks that you haven't approved this document yet. If you have not, then it selects all the approvers less than you, and gets the count of them who have not yet approved this document, if that count is zero then the document passes.

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