In here i want to, If AvailCode comes as a null
or empty
string then i need to show it "Temporary Unavailable".But my coding doen't show that one. (Consider only Avail Code).
var _staff = trv.GetBookDetails("4500").Select(b => new
{
value = b.bookno,
text = b.bookname + " " + "/"+" " + b.AvailCode ?? "TemporaryUnavailable",
});
the ?? operator only handles the NULL
case, not the empty case
replace
b.AvailCode ?? "TemporaryUnavailable"
with
string.IsNullOrEmpty(b.AvailCode)? "TemporaryUnavailable" : b.AvailCode
so the correct line would be
text = b.bookname + " / " + (string.IsNullOrEmpty(b.AvailCode) ? "TemporaryUnavailable" : b.AvailCode),
??
operator which called null-coalescing operator returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
This won't check for empty;
Use string.IsNullOrWhitespace
method instead.
string.IsNullOrWhitespace(b.AvailCode) ? "TemporaryUnavailable" : b.AvailCode
Operator ??
has very low precedence, it's evaluated after the +
operators on the left side. Therefore you can never really get null
on the left side. You need to wrap into parenthesis:
text = b.bookname + " " + "/"+" " + (b.AvailCode ?? "TemporaryUnavailable"),
or, in case you want to handle also empty:
text = b.bookname + " " + "/"+" " + (string.IsNullOrEmpty(b.AvailCode) ? "TemporaryUnavailable" : b.AvailCode),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.