[英]LINQ: How to modify the return type of AsParallel depending on a context?
[英]LINQ - How to modify the select depending on value that DB would return
我是Linq的新手,我正在使用.NET開發一個函數,該函數使用linq查詢的選定字段實例化POCO 。
問題在於這些字段之一必須在“運行時”進行轉換。
這是我的故障示例:
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
和
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
問題行是當我為屬性AfbInfection分配值時。 此屬性將與數據庫中的PotentialInfection值相反。 因此,如果I.PotentialInfection為True,則我的屬性AfbInfection應該為False。 如上所述執行此操作將導致IndexOutOfRangeException-索引不在數組的范圍內,不確定LinQ使用該NOT表達式正在做什么,但是肯定不是我想要的。
將數據庫字段值存儲到我的自定義對象中時,有什么方法可以修改它?
謝謝!
嘗試這個:
Private Shared Function GetInfectionHistory(HiveId As Long) As IList(Of HiveInfectionDetail)
Using DB As LandDataModelUnitOfWork = Context.CreateUnitOfWork
Dim historyQuery1 = From I In DB.HiveAfbInfections
Where I.HiveId = HiveId
Select New With {
.DateInfected = I.DateInfected,
.DateCleaned = I.DateCleared,
.PotentialInfection = I.PotentialInfection
}
Dim historyQuery2 = From I In historyQuery1.ToArray()
Select New HiveInfectionDetail With {
.DateInfected = I.DateInfected,
.DateCleaned = I.DateCleaned,
.PotentialAfbInfection = I.PotentialInfection,
.AfbInfection = Not I.PotentialInfection
}
Dim historyResult = historyQuery2.ToList()
If IsListEmpty(historyResult) Then
Return Nothing
End If
Return historyResult
End Using
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.