工作流中的复杂 LINQ 查询
我们有一个“文档”表。每份文件必须在生效前获得批准。
文件审批过程需要多个参与者。例如,设计师第一批准,执行经理第二批准,总经理第三批准。总经理不能先行批准。
该文档有一组批准者。 “批准者”具有“索引”、“批准者名称”和“已批准”属性。
问题是如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 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...
如果我正确地理解了这个问题:
它的作用是针对每个文档,它会在批准者列表中找到您,并检查您是否尚未批准该文档。如果您还没有批准,那么它会选择所有比您少的批准者,并获取尚未批准该文档的人数,如果该计数为零,则该文档通过。
If I understand the problem correctly:
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.