Is there a better way to implement this? Should I worry about the efficiency of the compilation method here?
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
The reason I want the lambda expression is because I want this function to be able to go through LINQ-to-SQL completely in order to convert it to SQL. The reason I need the direct version is because I have another code that I want to check for individual serial numbers before submitting the changes. And I'm afraid that GetValidSerialNumbers.Contains(serialNumber)will fulfill a more complex than the required request.
source
share