1

我正在JOIN输入两个 DataTales(通过解析两个 Excel 电子表格填充)。这些 Excel 电子表格包含的列TOTAL_DOLLARS并不TOTAL_UNITS总是有值。因此,当我尝试运行时,我的程序会抛出以下错误query.CopyToDataTable()

DataSet does not support System.Nullable<>

Dim query = From c In dtDollars.AsEnumerable() _
   Join r In dtUnits.AsEnumerable() _
   On c.Field(Of String)("UPC") Equals r.Field(Of String)("UPC") _
   Select _
   New _
   With {.UPC = r.Field(Of String)("UPC"), .WIC_NUMBER = c.Field(Of String)("WIC_NUMBER"), _
   .WAG_ITEM_DESC = c.Field(Of String)("WAG_ITEM_DESC"), _
   .WAG_BRAND = c.Field(Of String)("WAG_BRAND"), .UOM = c.Field(Of String)("UOM"), _
   .GSK_PROMO_GRP = c.Field(Of String)("GSK_PROMO_GRP"), _
   .TOTAL_DOLLARS = c.Field(Of Decimal?)("TOTAL_DOLLARS"), _
   .TOTAL_UNITS = r.Field(Of Integer?)("TOTAL_UNITS"), _
   .WKND_DATE = c.Field(Of DateTime)("WKND_DATE")}

    myResultTable = query.CopyToDataTable()

我能做些什么来解决这个问题?有没有办法可以将这些列的默认值设置为 0 if = DBNull.Value

这是我正在使用的Modules方法:ExtensionsCopyToDataTable()

Public Module CustomLINQtoDataSetMethods
    <Extension()> _
    Public Function CopyToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
        Return New ObjectShredder(Of T)().Shred(source, Nothing, Nothing)
    End Function

    <Extension()> _
    Public Function CopyToDataTable(Of T)(ByVal source As IEnumerable(Of T), ByVal table As DataTable, ByVal options As LoadOption?) As DataTable
        Return New ObjectShredder(Of T)().Shred(source, table, options)
    End Function

End Module

Module DataSetLinqOperators
  ''' <summary>
  ''' Creates a <see cref="DataTable"/> that contains the data from a source sequence.
  ''' </summary>
  ''' <remarks>
  ''' The initial schema of the DataTable is based on schema of the type T. All public property and fields are turned into DataColumns.
  ''' If the source sequence contains a sub-type of T, the table is automatically expanded for any addition public properties or fields.
  ''' </remarks>
  <Extension()> _
  Public Function ToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
    Return New ObjectShredder(Of T)().Shred(source, Nothing, Nothing)
  End Function

  ''' <summary>
  ''' Loads the data from a source sequence into an existing <see cref="DataTable"/>.
  ''' </summary>
  ''' <remarks>
  ''' The schema of <paramref name="table" /> must be consistent with the schema of the type T (all public property and fields are mapped to DataColumns).
  ''' If the source sequence contains a sub-type of T, the table is automatically expanded for any addition public properties or fields.
  ''' </remarks>
  <Extension()> _
  Public Function LoadSequence(Of T)(ByVal source As IEnumerable(Of T), ByVal table As DataTable, ByVal options As System.Nullable(Of LoadOption)) As DataTable
    If table Is Nothing Then
      Throw New ArgumentNullException("table")
    End If
    Return New ObjectShredder(Of T)().Shred(source, table, options)
  End Function
End Module

此外,我正在使用ObjectShredder类。

任何帮助深表感谢!

4

1 回答 1

1

试试这个:

.TOTAL_DOLLARS = If( (c.Field(Of Decimal?)("TOTAL_DOLLARS")).HasValue, _
                     (c.Field(Of Decimal?)("TOTAL_DOLLARS"), 0))

这有点尴尬,但它应该工作。

于 2012-12-21T22:47:09.657 回答