![](/img/trans.png)
[英]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.