E
E
embiid2022-03-22 17:46:06
C++ / C#
embiid, 2022-03-22 17:46:06

How to return result with empty value?

There is a function that accepts a value that will be checked against the value from the entity.

public async Task<long?> GetId(long bookingId)
{
      var bookPriceDb = await context.BookPrice.SingleOrDefaultAsync(x => x.BookingId == bookingId);
      var Id = bookingId;

      return Id;
}


There is a possibility that the values ​​will not be found => what to do with the check:
if(bookPriceDb == null)
{
???
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
freeExec, 2022-03-22
@freeExec

What to guess, you can clearly return found / not found

public async Task<(bool, long)> GetId(long bookingId)

G
GavriKos, 2022-03-22
@GavriKos

How does your architecture imply error handling?
Throw an exception. Return Nan/Infinity/-1. Return not long, but something that can be nullable (although it seems like from some kind of sisharp this can be stirred up with simple types)

M
Morrowind, 2022-03-22
@hekkaaa

Usually in such cases, an error is thrown and handled higher in the code, or in place if the value is null.

if(bookPriceDb == null)
{
 throw new NullReferenceException ("не существует указанного id: {bookingId} ")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question