Skip to content

Support for nullable data types#40

Open
zfriedrich wants to merge 1 commit into
laingsimon:masterfrom
zfriedrich:zf-allow-nullable
Open

Support for nullable data types#40
zfriedrich wants to merge 1 commit into
laingsimon:masterfrom
zfriedrich:zf-allow-nullable

Conversation

@zfriedrich

@zfriedrich zfriedrich commented Feb 16, 2022

Copy link
Copy Markdown

Hello, I noticed that nullable DateTime properties were not being copied in ObjectExtensions.GetDataReader. This change should add support for any properties of complex types that are nullables of a primitive type.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this pull request, great idea. I've made a suggestion for a simplification when nullable types are used.

var properties = elementType
.GetProperties()
.Where(p => IsPrimitiveType(p.PropertyType))
.Select(p => new { Property = p, NullablePrimitiveType = NullablePrimitiveType(p.PropertyType) })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest making a change more along the lines of:

.Select(p => new {
  name = p.Name,
  dataType = p.PropertyType,
  nullableDataType = NullablePrimativeType(p.PropertyType) != null,
})

In doing so the for loop later can be simplified:

foreach (var property in properties)
{
  var columnType = nullableDataType ?? property.PropertyType;
  var column = new DataColumn(property.name, columnType) 
  {
    AllowDBNull = nullableDataType != null
  }
  dataTable.Columns.Add(column);
}

Or something to that effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant