Linq为日期列强类型化DataTable和Null问题

时间:2014-09-17 11:58:47

标签: vb.net linq datatable

这是我的代码

 Dim dr2 As PBRuleData.PBRuleRow
                        Dim query As IEnumerable(Of PBRuleData.PBRuleRow) = From s In Me.PBRule.PBRule.AsEnumerable() _
                                                                            Where s.PBRuleId = .PBRuleId And s.StartDate <= .PBDate _
                                                                            And (s.IsFinishDateNull Or (s.FinishDate > .PBDate)) Select s

                        If query.Count > 0 Then
                            dr2 = query.ElementAt(0)
                            If Not (dr2.IsHoursNull) Then
                                If dr2.IsBilling Then
                                    dblHoursBILL += CType(dr2.Hours, Double)
                                Else
                                    dblHoursPAY += CType(dr2.Hours, Double)
                                End If
                            End If
                        End If

即使我已经放置了s.IsFinishDateNull条件以避免null异常但它仍然导致异常The value for column 'FinishDate' in table 'PBRule' is DBNull. 此linq查询是此数据表查询的替代查询,可以正常工作。

Dim dr As DataRow()
                        Dim sQuery As String
                        sQuery = "PBRuleId={0} AND StartDate<=#{1:MM/dd/yy}# AND (FinishDate>=#{1:MM/dd/yy}# OR FinishDate IS NULL)"
                        dr = Me.PBRule.PBRule.Select(String.Format(sQuery, .PBRuleId, .PBDate))

                        If dr.Length > 0 Then
                            If Not (dr(0).Item("Hours") Is DBNull.Value) Then
                                If dr(0).Item("IsBilling").ToString().ToLower = "true" Then
                                    dblHoursBILL += CType(dr(0).Item("Hours").ToString(), Double)
                                Else
                                    dblHoursPAY += CType(dr(0).Item("Hours").ToString(), Double)
                                End If
                            End If
                        End If

任何人都可以提供帮助

1 个答案:

答案 0 :(得分:0)

使用OrElse代替Or

无论左侧的结果如何,

Or运算符都会对表达式的两边进行求值。