c#-对象引用未设置为对象的实例-LINQ

c#-对象引用未设置为对象的实例-LINQ,c#,asp.net,linq,model-view-controller,C#,Asp.net,Linq,Model View Controller,我正在尝试创建一个视图,用户可以在其中查看他们所做的预订。我有一个模型,上面有两种不同设施的预订清单,一个大厅和一个房间。因此,该模型包含一个大厅预订列表和一个房间预订列表。在我的控制器中,我试图将所有大厅预订添加到大厅预订列表中,房间预订也是如此。但我得到了这个错误 “对象引用未设置为对象的实例。” 这是我的模型: public class AllBookingsViewModel { public List<HallActivityBooking> HallBookin

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

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

这是我的模型:

public class AllBookingsViewModel
{

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

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

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

foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id).FirstOrDefault())
{
    model.HallBookingsList.Add(hallBooking);
}
        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);
        };  
这给了我一个错误:

严重性代码说明项目文件行抑制状态 错误CS0446 Foreach无法对“方法组”进行操作。是吗 是否要调用“方法组”。。。26活跃

我们将不胜感激


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

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

foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id).FirstOrDefault())
{
    model.HallBookingsList.Add(hallBooking);
}
        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);
        };  

我认为这里发生的是,您可能有一个
大厅预订
房间预订
,其中
客户用户
为空

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

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

例如:

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

您需要初始化属性

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

每个
HallBookings
记录是否都有一个与之关联的
CustomerUser
?从您提到的错误消息null reference exception中,我可以假设至少有一个
HallBookings
具有作为
CustomerUser
的null引用。FirstOrDefault只返回一个与Where子句的条件匹配的对象。所以你不能用foreach。哪一行代码给出了空引用错误?
AllBookingsViewModel
中的集合未初始化。是的,一个HallBooking有一个CustomerUser,此时只有一个booking,并且没有空CustomerUser引用。如何初始化所有BookingsViewModel?除非有预订,否则它不应该包含任何内容,如果我初始化它,那么就在bookings视图中显示初始化的预订?它应该只显示存在的预订。是的,我使用FirstOrDefault()计算了很多,这只是我尝试过的东西,但我删除了它。是的,我基本上需要初始化它。基本上foreach是不必要的,下面是我所做的:AllBookingsViewModel=new AllBookingsViewModel();{model.HallBookingsList=db.HallBookings.Where(x=>x.CustomerUser.Id==currentUser.Id);model.RoomBookingsList=db.RoomBookings.Where(x=>x.CustomerUser.Id==currentUser.Id);};问题解决了。非常感谢。
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);