得到错误“使用未分配的局部变量”

时间:2019-02-08 15:23:56

标签: c# asp.net-mvc if-statement

我有一个功能,其中有基于该模式的三种模式,我在视图中显示数据库记录,因为我正在使用以下代码,但出现以下错误

  

使用未分配的局部变量“结果”

这是我的控制器代码

public PartialViewResult _LaundryBookingPart(DateTime? From, DateTime? To, int mode)
{
    List<RoomLaundryvw> result;
    if (mode == (int)ItemTypesEnum.LaundryGuest)
    {
        result = db.Query<RoomLaundryvw>($"Select rm.RoomID,rm.RoomNo,g.GuestID,g.GuestName,ln.LaundryID,it.ItemName,ln.PickDate,ln.ServiceKind,ln.DropQty,ln.DropDate from Laundry ln " +
        "inner join Items it on ln.ItemId = it.ItemId " +
        "inner join Rooms rm on ln.RoomID = rm.RoomID " +
        "left join Reservation_Guest rg on rg.ReservationID = ln.ReservationID " +
        $"left join Guests g on g.GuestID = rg.GuestID").ToList();
    }

    if (mode == (int)ItemTypesEnum.LaundryStaff)
    {
        result = db.Query<RoomLaundryvw>("Select au.RealName as GuestName from Laundry ln INNER JOIN AspNetUsers au ON ln.UserId = au.Id").ToList();
    }

    if (mode == (int)ItemTypesEnum.Linen)
    {
        result = db.Query<RoomLaundryvw>("Select rm.RoomID,rm.RoomNo,g.GuestID,it.ItemName,g.GuestName from Laundry ln " +
                 "inner join Items it on ln.ItemId = it.ItemId " +
                 "inner join Rooms rm on ln.RoomID = rm.RoomID " +
                 "left join Reservation_Guest rg on rg.ReservationID = ln.ReservationID " +
                 "left join Guests g on g.GuestID = rg.GuestID ").ToList();
    }
    return PartialView(result);
}

我收到上述错误的红线。

请帮助我。

1 个答案:

答案 0 :(得分:0)

问题在于您永远不会为结果指定默认值。

IE:您的If语句都不可能执行和初始化结果变量。