0

I have this EF 6 query:

var semana = servicio.Semana.Where(s => s.SemanaDia.Equals(ds.Dia)).FirstOrDefault();

Where ds.Dia is an integer an equal to 2. servicio is an entity and semana is a related table.

When debugging, I see that servicio.Semana contains a value that matchs the criteria, however, that query returns null.

If I replace .Equals by ==, the record is retrieved and assigned to semana variable.

What may be the explanation to this?

SemanaDia is also an integer in the database.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • is any of these fields a nullable field? – Pablo CG May 23 '20 at 00:37
  • What is the type of `SemanaDia`? Is it string? Are you comparing an integer with a string? – Hooman Bahreini May 23 '20 at 00:59
  • Please [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. Then see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query for SQL related questions. – Progman May 23 '20 at 09:50
  • 1
    This is not a duplicate of https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals As that deals with the difference of the two in code, here it's about how they are translated to SQL by EF. – juharr May 23 '20 at 12:00
  • 1
    I'd recommend looking at the SQL that is generated by the two. You can do that by doing `servicio.Semana.Where(s => s.SemanaDia.Equals(ds.Dia)).ToString()`, then change it to use `==` and see the difference in the SQL. – juharr May 23 '20 at 12:10
  • @HoomanBahreini you will have the answer in the question itself. – jstuardo May 23 '20 at 23:45
  • @PabloCG no.... database field is `int not null` – jstuardo May 23 '20 at 23:46
  • 1
    @jstuardo I mean in c# code, are they `int` or `int?` ? – Pablo CG May 23 '20 at 23:50
  • @Progman This involves database. The difference is only when trying to compare an int field in the database with an int variable. It is not very usefull to try to do a test case. – jstuardo May 23 '20 at 23:53
  • @juharr Good idea. I don't have time right now, but I will do that soon. – jstuardo May 23 '20 at 23:55
  • Database field: `int not null`. Integer variable: `int` – jstuardo May 23 '20 at 23:56
  • 1
    @jstuardo If you provide a MCVE it is possible that everyone can test it and maybe even provide an answer to the problem. `Equals()` should work unless you have done something somewhere we currently can't see. – Progman May 24 '20 at 00:23
  • Finally, I have seen there is no related SQL statement with the filter. The query is performed in the statement before the comparison. The instruction before that is using `servicio.Semana`. When EF reachs that instruction, the SQL query is performed filling all records of `servicio.Semana` related property `ICollection`. Well, the only difference with the returned data is this: returned `semana.SemanaDia` is of type `short` while `ds.Dia` is of type int. So, maybe the question should be: why `Equals` method does not work well when comparing `short` variable to `int` variable. – jstuardo May 26 '20 at 16:45
  • Please see: https://dotnetfiddle.net/klHd8q. I have used that fiddle to test. I finally read this: https://stackoverflow.com/questions/33362737/equals-int-and-short-c-sharp-returns-false that explains why. – jstuardo May 26 '20 at 17:00

0 Answers0