Reputation: 27
I have this piece of code where I do the validation before a null variable, however I get this error in SonarQube, I would like to know how to solve.
public int? GetCityId(string cityName, string stateName)
{
if (_cityData == null) throw new InvalidOperationException("You have to initialize the data first.");
if(!string.IsNullOrEmpty(stateName))
{
_cityData.Where(x => x.StateName == stateName.ToUpper() || x.StateInitials == stateName.ToUpper());
}
return _cityData.Where(x =>
x.CityName == cityName.ToUpper() || x.CityAlias == cityName.ToUpper())
.Select(x => x.Id)
.FirstOrDefault();
}
This is the error in SonarQube: enter image description here
Upvotes: 0
Views: 1120
Reputation: 11100
Did you mean to apply two calls to .Where
to your query, then run it?
var query = _cityData.Where(x =>
x.CityName == cityName.ToUpper() || x.CityAlias == cityName.ToUpper());
if(!string.IsNullOrEmpty(stateName))
query = query.Where(x => x.StateName == stateName.ToUpper() || x.StateInitials == stateName.ToUpper());
return query
.Select(x => x.Id)
.FirstOrDefault();
Upvotes: 1