I have a method right now that loops through a list of business objects (property Properties) to test if property SerialNumber is a serial number or not. If I find a serial number, I exit the loop and return true, otherwise I return false.
Code is as follows:
public bool HasSerialNumber()
{
if (this.Properties != null && this.Properties.Count > 0)
{
foreach (var property in Properties)
{
if (!string.IsNullOrEmpty(property.SerialNumber))
return true;
}
}
return false;
}
Is there a better LINQ approach to this?
I have the following in mind:
return Properties.Where(x => !string.IsNullOrEmpty(x.SerialNumber)).ToList().Count > 0;
Is there a better/faster method for checking for non-empty string?
You can use
Anyinstead of checking if the count is greater than zero.and of course your
Properties.Count > 0check is redundant.