Database ExtensionsNull If Empty Method
This is used to convert strings to null values if they are empty
Definition
Namespace: EWSoftware.EntityFramework
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
If the value is null or an empty string this returns null. If it is not null or an empty string, it returns the passed value.
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
C#
public static string? NullIfEmpty(
this string? value
)VB
<ExtensionAttribute>
Public Shared Function NullIfEmpty (
value As String
) As StringC++
public:
[ExtensionAttribute]
static String^ NullIfEmpty(
String^ value
)F#
[<ExtensionAttribute>]
static member NullIfEmpty :
value : string -> string Parameters
- value String
- The string value to check
Return Value
StringIf the value is null or an empty string this returns null. If it is not null or an empty string, 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 String. 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 string values to database context methods when the value needs to
be stored as a null value rather than an empty string.
Example
C#
using var dataContext = new MyDbContext();
dataContext.spOTWantedAddEdit(overtimeKey, entityKey,
dtpBeginDate.Value, dtpEndDate.Value, (byte)days,
(byte)shifts, txtWorksiteNote.Text.NullIfEmpty(),
txtRequestNote.Text.NullIfEmpty());