【问题标题】:c# - object reference not set to an instance of an object - LINQc# - 对象引用未设置为对象的实例 - LINQ
【发布时间】:2017-06-03 15:31:17
【问题描述】:

我正在尝试创建一个视图,用户可以在其中看到他们所做的预订。我有一个模型,其中包含在不同设施、大厅和房间中的 2 种不同类型的预订列表。因此,该模型包含一个大厅预订列表和一个房间预订列表。在我的控制器中,我试图将所有大厅预订添加到大厅预订列表中,房间预订也是如此。但是我收到了这个错误

“对象引用未设置为对象的实例。”

这是我的模型:

public class AllBookingsViewModel
{

    public List<HallActivityBooking> HallBookingsList { get; set; }

    public List<RoomBooking> RoomBookingsList { get; set; }

}

这是我的控制器:

public class BookingsController : BaseController
{
    // GET: Bookings
    public ActionResult Bookings()
    {

        //Get current user
        var userId = User.Identity.GetUserId();
        var currentUser = db.Users.Find(userId);

        AllBookingsViewModel model = new AllBookingsViewModel();

        //Fill activity booking
        foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id))
        {
            model.HallBookingsList.Add(hallBooking); //<< This is where I get an exception
        }


        //Fill room bookings list
        foreach (RoomBooking roomBooking in db.RoomBookings.Where(x => x.CustomerUser.Id == currentUser.Id))
        {
            model.RoomBookingsList.Add(roomBooking);
        }

        return View(model);
    }

这是我尝试过的(这些不再在我的代码中):

我尝试过像这样声明模型:

var model = new AllBookingsViewModel

我尝试在 linq 语句中添加“FirstOrDefault”:

foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id).FirstOrDefault())
{
    model.HallBookingsList.Add(hallBooking);
}

这给了我这个错误:

严重性代码描述项目文件行抑制状态 错误 CS0446 Foreach 无法对“方法组”进行操作。你是否 打算调用“方法组”? ... 26 活跃

我们将不胜感激


已解决

这是我为解决这个问题所做的:

for-each 不是必需的,我只需要通过给它 LINQ 语句的值来初始化视图模型:

        AllBookingsViewModel model = new AllBookingsViewModel();
        {
            model.HallBookingsList = db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id);
            model.RoomBookingsList = db.RoomBookings.Where(x => x.CustomerUser.Id == currentUser.Id);
        };  

【问题讨论】:

  • 每条HallBookings 记录是否都有与之关联的CustomerUser?根据您提到的错误消息,空引用异常,我可以假设至少一个 HallBookings 具有作为 CustomerUser 的空引用。
  • FirstOrDefault 只返回一个符合 Where 子句条件的对象。所以你不能在上面使用 foreach 。哪一行代码给出了空引用错误?
  • AllBookingsViewModel 中的集合未初始化。
  • 是的,HallBooking 有一个 CustomerUser,此时只有 1 个预订并且它没有空 CustomerUser 引用。我将如何初始化 AllBookingsViewModel?除非有预订,否则它不应该有任何东西,如果我初始化它不会那么只是在预订视图中显示初始化的预订?它应该只在存在时显示预订。
  • 是的,我想尽可能多地使用 FirstOrDefault(),这只是我尝试过的,但我删除了它。

标签: c# asp.net linq model-view-controller


【解决方案1】:

你需要初始化属性

AllBookingsViewModel model = new AllBookingsViewModel();
model.HallBookingsList = new List<HallActivityBooking>();
model.RoomBookingsList = new List<RoomBooking>();  
//Your existing code

或者,您可以直接使用 LINQ 结果来设置这些属性。

AllBookingsViewModel model = new AllBookingsViewModel();
model.HallBookingsList = db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id);
model.RoomBookingsList = db.RoomBookings.Where(x => x.CustomerUser.Id == currentUser.Id);

【讨论】:

  • 是的,我基本上需要初始化它。基本上 foreach 是不必要的,这就是我所做的: AllBookingsViewModel model = new AllBookingsViewModel(); { model.HallBookingsList = db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id); model.RoomBookingsList = db.RoomBookings.Where(x => x.CustomerUser.Id == currentUser.Id); };问题解决了。谢谢
【解决方案2】:

我认为这里发生的情况是,您的HallBookingsRoomBookings 中的一个CustomerUser 可能为null。

因此,当 LINQ 执行您在 Where 子句中传递的 Func 时,您的 CustomerUser 为 null 的实体之一会导致错误。

我不知道您的业务逻辑是否允许这样做,但是您可以在 Where 中提供的 Func 中提供一些 null 检查以避免那些 null 引用。

例子:

//Fill activity booking
foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser != null ? x.CustomerUser.Id == currentUser.Id : false))
{
    model.HallBookingsList.Add(hallBooking);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多