Hi im trying to do a string match for some certain things in my webservice however I get stuck at comparing a double
public List<Customers> GetCustomer(string anything)
{
List<Customers> customers = customermembers.Where(n =>
string.Equals(n.CustomerID, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(n.FirstName, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(n.LastName, anything, StringComparison.CurrentCultureIgnoreCase)
|| double.Equals(n.Age, anything, StringComparison.CurrentCultureIgnoreCase) //this like
).ToList();
return customers;
}
How do you compare a double?
My DataContract looks like this:
[DataMember(Name = "Age")]
public double Age { get; set; }
I assume that you want to compare two double values representing age and that
anythingis indeed not astringbut adouble(it is a bit hard to tell from the question because the wayanythingis used doesn’t make much sense).Best thing you can do is to switch the type of
Agefromdoubletodecimal(or even betterintif the age is an integer). Doing that avoid rounding errors.However, if you want to stick to floating point numbers you can simply compare two
doublevalues using==:or use the static
object.Equalsmethod:If you perform calculations on the ages you will probably get rounding problems and then you will have to use a small epsilon value that represents the rounding precision you want to use: