【问题标题】:LINQ - How to modify the select depending on value that DB would returnLINQ - 如何根据 DB 返回的值修改选择
【发布时间】:2014-08-18 03:22:23
【问题描述】:

我对 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 表达式做了什么,但肯定不是我想要的。

在将数据库字段值存储到我的自定义对象中时,有什么方法可以修改它?

谢谢!

【问题讨论】:

  • 我也试过 .AfbInfection = (I.PotentialInfection = False) 没有成功。检查是否有结果时,它会抛出相同的 IndexOutOfRangeException。

标签: .net vb.net linq lightspeed


【解决方案1】:

试试这个:

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

【讨论】:

  • 谢谢。您的解决方案工作正常,但我想知道是否有可能使用在运行时修改所选字段的指令构建 LINQ。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
相关资源
最近更新 更多