.net LINQ-如何根据DB将返回的值修改select
我对Linq比较陌生,我正在使用.NET开发一个函数,该函数用Linq查询的选定字段实例化一个实例 问题是其中一个字段必须“在运行时”转换 这是我的故障示例:.net LINQ-如何根据DB将返回的值修改select,.net,vb.net,linq,lightspeed,.net,Vb.net,Linq,Lightspeed,我对Linq比较陌生,我正在使用.NET开发一个函数,该函数用Linq查询的选定字段实例化一个实例 问题是其中一个字段必须“在运行时”转换 这是我的故障示例: Private Shared Function GetInfectionHistory(HiveId As Long) As IList(Of HiveInfectionDetail) Using DB As LandDataModelUnitOfWork = Context.CreateUnitOfWork
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
及
有问题的一行是当我给属性afbinefection赋值时。此属性将与数据库中的PotentialInfection值相反。所以如果I.PotentialInfection为真,那么我的属性afbininfection应该为假。如上所述操作将导致IndexOutOfRangeException-Index超出了数组的边界不确定LinQ对该not表达式做了什么,但肯定不是我所希望的
在将DB字段值存储到我的自定义对象中时,有没有办法修改它
谢谢大家! 试试这个:
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
我还尝试了.afbinefection=(I.PotentialInfection=False),但没有成功。当检查是否有结果时,它抛出相同的IndexOutOfRangeException。谢谢。您的解决方案工作得很好,但我想知道是否有可能在运行时使用修改所选字段的指令构建LINQ。
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