I have this method
private IList<Order> FilterOrders(string filterText)
{
string filterTextLowerCase = filterText.ToLower();
var filtered = _orders.Where(order =>
order.OrderIdFullOrderNumber.ToLower().Contains(filterTextLowerCase) ||
order.Name.ToLower().Contains(filterTextLowerCase) ||
order.Status.ToLower().Contains(filterTextLowerCase) ||
order.TimeRemaining.ToLower().Contains(filterTextLowerCase) ||
order.Address.ToLower().Contains(filterTextLowerCase) ||
order.City.ToLower().Contains(filterTextLowerCase) ||
order.State.Abbrev.ToLower().Contains(filterTextLowerCase) ||
order.PostalCode.ToLower().Contains(filterTextLowerCase)).ToList();
return filtered;
}
As you can see there's a possible nullreference exception in properties, what would be the best way to check for null in this Where expression?
Add this extension method:
public static string NullSafeToLower(this string s)
{
if (s == null)
{
s = string.Empty;
}
return s.ToLower();
}
Then replace calls to "ToLower" with calls to "NullSafeToLower."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With