Possible Duplicate:
Lambda Expression: == vs. .Equals()
Hi,
I use a lot the keyword Equals to compare variables and other stuff.
but
wines = wines.Where(d => d.Region.Equals(paramRegion)).ToList();
return an error at runtime when in the data Region is NULL
I had to use the code
wines = wines.Where(d => d.Region == paramRegion).ToList();
to get rid of the error.
Any ideas why this raises an error?
Thanks.
You cannot call instance methods with null object reference. You should check that the Region is not null before calling its instance methods.
wines = wines.Where(d => d.Region != null && d.Region.Equals(paramRegion)).ToList();
The d.Region == paramRegion
is (most likely) expanded to object.Equals(d.Region, paramRegion)
and that static method does check whether the parameters are null or not before calling the Equals() method.
You can also write the condition in different order if you know that the paramRegion
cannot be null.
Debug.Assert(paramRegion != null);
wines = wines.Where(d => paramRegion.Equals(d.Region)).ToList();