使用 LINQ SUM 时出现异常

发布于 2024-11-01 21:11:35 字数 425 浏览 1 评论 0原文

我试图获取“bookings”的总和,但收到错误“转换为值类型‘Int32’失败,因为物化值为空。结果类型的通用参数或查询必须使用可为空的类型。”

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                     x.StartDate <= bookingEnd &&
                                     x.EndDate >= bookingStart)
                                    .Sum(x => x.BookingQuantity);

我该如何解决这个问题?如果它的预订为空,我需要得到 0。

I'm trying to get the SUM of "bookings" and I get error "The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                     x.StartDate <= bookingEnd &&
                                     x.EndDate >= bookingStart)
                                    .Sum(x => x.BookingQuantity);

How shall I fix this? I need to get 0 if it ever gets to be null else its bookings.

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

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

发布评论

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

评论(3

断念 2024-11-08 21:11:35

尝试使用 null 合并运算符:

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                 x.StartDate <= bookingEnd &&
                                 x.EndDate >= bookingStart && 
                                 x.BookingQuantity != null)
                                .Sum(x => (int?)x.BookingQuantity) ?? 0;

或者将 bookings 声明为可空 int

int? bookings = ...

编译器类型推断将 Sum 的结果拾取为普通 int,它永远不应该为 null。

Try the null coalescing operator:

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                 x.StartDate <= bookingEnd &&
                                 x.EndDate >= bookingStart && 
                                 x.BookingQuantity != null)
                                .Sum(x => (int?)x.BookingQuantity) ?? 0;

or declare bookings as a nullable int

int? bookings = ...

The compilers type inference is picking up the result of Sum as a plain int, which should never be null.

落墨 2024-11-08 21:11:35

此页面建议解决此问题;

Sum(x => (int?)x.BookingQuantity) ?? 0;

This page suggests a fix to this problem;

Sum(x => (int?)x.BookingQuantity) ?? 0;
淡淡绿茶香 2024-11-08 21:11:35

添加空值检查。

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                     x.StartDate <= bookingEnd &&
                                     x.EndDate >= bookingStart && 
                                     x.BookingQuantity != null)
                                    .Sum(x => x.BookingQuantity);

Add checking for null.

var bookings = entities.Bookings.Where(x => x.ID == id &&
                                     x.StartDate <= bookingEnd &&
                                     x.EndDate >= bookingStart && 
                                     x.BookingQuantity != null)
                                    .Sum(x => x.BookingQuantity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文