有没有更好的方法来实现这一点?我应该关注 Compile 方法的性能吗?
Public Overrides Function GetValidSerialNumbers() As System.Collections.Generic.IEnumerable(Of SerialNumber)
Return Queryable.Where(allSerials, SerialNumberValidFunc)
End Function
Public Overrides Function IsSerialNumberValid(serialNumber As SerialNumber) As Boolean
Return (SerialNumberValidFunc.Compile().Invoke(serialNumber))
End Function
Private ReadOnly Property SerialNumberValidFunc As Expressions.Expression(Of Func(Of SerialNumber, Boolean))
Get
If ProductionReceiptLine.MOOutput Is Nothing Then
Return Function(sn As SerialNumber) sn.ItemInventory Is Nothing AndAlso _
(sn.Status = SerialNumberStatusValues.Planned AndAlso sn.MO Is ProductionReceiptLine.ProductionReceipt.MO _
OrElse sn.Status = SerialNumberStatusValues.Assigned)
Else
Return Function(sn As SerialNumber) sn.ItemInventory Is Nothing AndAlso _
(sn.Status = SerialNumberStatusValues.Planned AndAlso sn.MO Is Nothing OrElse sn.Status = SerialNumberStatusValues.Assigned)
End If
End Get
End Property
我想要 lambda 表达式的原因是因为我希望该函数能够一直通过 LINQ-to-SQL 转换为 SQL。我想要直接版本的原因是因为我有其他代码要在提交更改之前验证各个序列号。而且我担心这GetValidSerialNumbers.Contains(serialNumber)
会执行比必要更复杂的查询。