开发者

Can I configure a strongly typed data set to use nullable values?

开发者 https://www.devze.com 2022-12-28 19:05 出处:网络
If I have a strongly typed data table with a column for values of type Int32, and 开发者_开发问答this column allows nulls, then I\'ll get an exception if I do this for a row where the value is null:

If I have a strongly typed data table with a column for values of type Int32, and 开发者_开发问答this column allows nulls, then I'll get an exception if I do this for a row where the value is null:

int value = row.CustomValue;

Instead I need to do this:

if (!row.IsCustomValueNull()) {
    int value = row.CustomValue;
    // do something with this value
}

Ideally I would like to be able to do this:

int? value = row.CustomValue;

Of course I could always write my own method, something like GetCustomValueOrNull; but it'd be preferable if the property auto-generated for the column itself simply returned a nullable. Is this possible?


Unfortunately, this is not supported.

However, you can make your own wrapper property, like this:

    public int? CustomValue {
        get { return IsCustomValueqlNull() ? new int?() : CustomValueSql; }
        set {
            if (value == null)
                SetCustomValueSqlNull();
            else
                CustomValueSql = value.Value;
        }
    }

Where CustomValueSql is the actually column name.

0

精彩评论

暂无评论...
验证码 换一张
取 消