Database ExtensionsTo NullableT(T) Method
This is used to convert value types to null values if they are set to their default value for the
type (i.e. zero for integers, DateTime.MinValue for date/times, etc).
Definition
Namespace: EWSoftware.EntityFramework
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
If the value is set to the default value for its type, this returns null. If not, it returns the passed value.
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
C#
public static T? ToNullable<T>(
this T value
)
where T : struct, new()
VB
<ExtensionAttribute>
Public Shared Function ToNullable(Of T As {Structure, New}) (
value As T
) As T?C++
public:
[ExtensionAttribute]
generic<typename T>
where T : value class, gcnew()
static Nullable<T> ToNullable(
T value
)F#
[<ExtensionAttribute>]
static member ToNullable :
value : 'T -> Nullable<'T> when 'T : struct, new()Parameters
- value T
- The value to check
Type Parameters
- T
- The data type to use
Return Value
NullableTIf the value is set to the default value for its type, this returns null. If not, it returns the passed value.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type T. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).Remarks
This is useful for passing values to database context methods when the parameters needs to
be stored as a null value rather than a literal value if it is set to the default.
Example
C#
using var dataContext = new MyDbContext();
dataContext.spUpdateStartDate(caseKey, startDate.ToNullable());