I'm relatively new to Linq and I'm developing with .NET a function that instantiates a POCO with the selected fields of the linq query.
The problem is that one of these fields must be transformed "at runtime".
This is my malfunctioning example:
Private Shared Function GetInfectionHistory(HiveId As Long) As IList(Of HiveInfectionDetail)
Using DB As LandDataModelUnitOfWork = Context.CreateUnitOfWork
Dim historyResult = From I In DB.HiveAfbInfections
Where I.HiveId = HiveId
Select New HiveInfectionDetail With {
.DateInfected = I.DateInfected,
.DateCleaned = I.DateCleared,
.PotentialAfbInfection = I.PotentialInfection,
.AfbInfection = Not I.PotentialInfection
}
If IsListEmpty(historyResult.ToList()) Then
Return Nothing
End If
Return historyResult.ToList()
End Using
End Function
and
Private Shared Function IsListEmpty(Of T)(source As IEnumerable(Of T)) As Boolean
If source Is Nothing Then
Return True
End If
Return Not source.Any()
End Function
enter code here
The problematic line is when I assign a value to the property AfbInfection. This property will be the opposite value to the PotentialInfection in database. So if the I.PotentialInfection is True, then my property AfbInfection should be False. Doing it as above will cause an IndexOutOfRangeException - Index was outside the bounds of the array not sure what LinQ is doing with that NOT expression, but certainly it's not what I wish.
Is there any way to modify the DB field value when storing it into my custom object?
Thank you!
Try this: