Using the equals keyword in linq

Possible duplicate:
Lambda expression: == vs. .Equals ()

Hi,

I use a lot of Equals keywords to compare variables and other things.

but

wines = wines.Where(d => d.Region.Equals(paramRegion)).ToList();

returns a runtime error when in a data region NULL

I had to use code

wines = wines.Where(d => d.Region == paramRegion).ToList();

to get rid of the error.

Any ideas why this is causing the error?

Thank.

+3
source share
3 answers

You cannot call instance methods with a null reference to an object. You must verify that the scope is not null before calling instance methods.

wines = wines.Where(d => d.Region != null && d.Region.Equals(paramRegion)).ToList();

d.Region == paramRegion(most likely) extended to object.Equals(d.Region, paramRegion)and this static method checks if the parameters are null or not before calling the Equals () method.

, , paramRegion .

Debug.Assert(paramRegion != null);
wines = wines.Where(d => paramRegion.Equals(d.Region)).ToList();
+3

,

d.Region == null

, Equals (...), , .

+3

Use may use:

paramRegion.Equals(d.Region)
0
source

Source: https://habr.com/ru/post/1788682/


All Articles