Linq 和 DBNull - 出现错误

发布于 2024-09-07 22:50:57 字数 774 浏览 1 评论 0原文

从 rows.AsEnumerable() 中进行选择时出现错误。我正在使用以下代码...

var rows = ds.Tables[0].AsEnumerable();
                trafficData = rows.Select(row => new tdDataDC
                {
                    CalculationCount = row.Field<Int64>("biCalculationCountSeqID")
                    , Zone = row.Field<Int16>("siFkZoneId")
                    , Miles = row.Field<decimal>("dcMiles")
                    , Plaza = row.Field<Int16>("siFkPlazaId")
                    , VehicleCount = row.Field<int>("iVehicleCount")


                });

大多数情况下它运行良好,但是当数据库中存在 NULL 时,我收到此错误“无法将 DBNull.Value 转换为类型“System.Int16”。请使用可空类型..” 我该如何纠正这个问题?我不希望我的数据契约具有可为 Null 的类型,我想使用三元或其他类型,如果值为 NULL,则只需使用 0。这可能吗?

感谢您的帮助,
〜ck

I'm getting an error when selecting from a rows.AsEnumerable(). I am using the following code...

var rows = ds.Tables[0].AsEnumerable();
                trafficData = rows.Select(row => new tdDataDC
                {
                    CalculationCount = row.Field<Int64>("biCalculationCountSeqID")
                    , Zone = row.Field<Int16>("siFkZoneId")
                    , Miles = row.Field<decimal>("dcMiles")
                    , Plaza = row.Field<Int16>("siFkPlazaId")
                    , VehicleCount = row.Field<int>("iVehicleCount")


                });

Most of the time it works well, but when there are NULLS in the database I'm getting this error "Cannot cast DBNull.Value to type 'System.Int16'. Please use a nullable type.."
How can I correct this? I don't want my datacontracts to have Nullable types, I'd like to use a ternary or something, and if a value is NULL, just use 0. Is this possible?

Thanks for any help,
~ck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

来世叙缘 2024-09-14 22:50:57

您始终可以添加另一个扩展方法(未经测试):

   public static T FieldOrDefault<T>(this DataRow row, string columnName)
   {
       return row.IsNull(columnName) ? default(T) : row.Field<T>(columnName);   
   }

然后您的调用站点如下所示:

var rows = ds.Tables[0].AsEnumerable();
                trafficData = rows.Select(row => new tdDataDC
                {
                    CalculationCount = row.FieldOrDefault<Int64>("biCalculationCountSeqID")
                    , Zone = row.FieldOrDefault<Int16>("siFkZoneId")
                    , Miles = row.FieldOrDefault<decimal>("dcMiles")
                    , Plaza = row.FieldOrDefault<Int16>("siFkPlazaId")
                    , VehicleCount = row.FieldOrDefault<int>("iVehicleCount")


                });

You could always add another extension method (untested):

   public static T FieldOrDefault<T>(this DataRow row, string columnName)
   {
       return row.IsNull(columnName) ? default(T) : row.Field<T>(columnName);   
   }

Then your callsite looks like:

var rows = ds.Tables[0].AsEnumerable();
                trafficData = rows.Select(row => new tdDataDC
                {
                    CalculationCount = row.FieldOrDefault<Int64>("biCalculationCountSeqID")
                    , Zone = row.FieldOrDefault<Int16>("siFkZoneId")
                    , Miles = row.FieldOrDefault<decimal>("dcMiles")
                    , Plaza = row.FieldOrDefault<Int16>("siFkPlazaId")
                    , VehicleCount = row.FieldOrDefault<int>("iVehicleCount")


                });
┈┾☆殇 2024-09-14 22:50:57

这是测试空值的方法...

Plaza = row.IsNull("siFkPlazaId") ? 0 : row.Field<int>("siFkPlazaId")

Here is how you test for nulls...

Plaza = row.IsNull("siFkPlazaId") ? 0 : row.Field<int>("siFkPlazaId")
伤感在游骋 2024-09-14 22:50:57

如果你的财产可以为空,你也可以这样做;

Plaza = row.Field<int16?>("siFkPlazaId")

If your property is nullable you can do this as well;

Plaza = row.Field<int16?>("siFkPlazaId")
聆听风音 2024-09-14 22:50:57

我非常喜欢 ?? 运算符:

CalculationCount = row.Field<Int64?>("biCalculationCountSeqID") ?? 0
                , Zone = row.Field<Int16?>("siFkZoneId") ?? 0
                , Miles = row.Field<decimal?>("dcMiles") ?? 0.0m
                , Plaza = row.Field<Int16?>("siFkPlazaId") ?? 0
                , VehicleCount = row.Field<int>("iVehicleCount") 0;

I'm pretty fond of the ?? operator:

CalculationCount = row.Field<Int64?>("biCalculationCountSeqID") ?? 0
                , Zone = row.Field<Int16?>("siFkZoneId") ?? 0
                , Miles = row.Field<decimal?>("dcMiles") ?? 0.0m
                , Plaza = row.Field<Int16?>("siFkPlazaId") ?? 0
                , VehicleCount = row.Field<int>("iVehicleCount") 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文