我需要知道什么?在这个if语句中意味着什么? asp.net核心2

时间:2018-06-20 08:55:21

标签: c# operators

任何人都可以帮助我使用此代码。我已经在asp.net MVC c#中编写了此代码,但是我不知道为什么使用它?在if语句逻辑中。我想知道什么是预订?.UserID ??

public async Task<IActionResult> Details(int id)
{
    //get the user who already logged in
    IdentityUser user = await 
    _userManagerService.FindByNameAsync(User.Identity.Name);

    //get single package 
    Booking booking = _bookingDataService.GetSingle(b => b.BookingID 
               == id);

    if ((booking?.UserID ?? "A") == (user?.Id ?? "B"))
    {
        //create vm
        BookingDetailsViewModel vm = new BookingDetailsViewModel
        {
            BookingDate=booking.BookingDate,
            Price=booking.Price,
            Qty=booking.Qty
        };
        //pass to view
        return View(vm);
        }
        else
        {
            return RedirectToAction("Index", "Customer");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

MyVar?.SomeProperty检查MyVar是否为空。

var foo = MyVar?.SomeProperty就像写var foo = ((MyVar == null) ? (null) : (MyVar.SomeProperty))

MyVar.SomeProperty ?? "SomeValue"检查SomeProperty是否为空,然后为其分配值“ SomeValue”

var foo = MyVar.SomeProperty ?? "SomeValue"就像写var foo = ((MyVar.SomeProperty == null) ? ("SomeValue") : (MyVar.SomeProperty))