0

when i used Equals() , not worked and returned null but after replace Equals() with Equality(==), It worked properly

var a = await _context.OrganizationalRoleAssignments
                            .Where(
                                 x => x.OrganizationalRoleId.Equals(organizationalRoleId)
                                  && x.UnassignUtcDate.Equals(null)
                                  && x.UnassignCause.Equals(null)
                             ).ToListAsync();



await _context.OrganizationalRoleAssignments
                            .Where(
                                 x => x.OrganizationalRoleId.Equals(organizationalRoleId)
                                  && x.UnassignUtcDate == null
                                  && x.UnassignCause == null
                             ).ToListAsync();
Rohullah Rajaee Rad
  • 571
  • 2
  • 9
  • 33
  • 2
    `Equals` != `==` :) https://stackoverflow.com/questions/1123888/linq-what-is-the-difference-between-and-equals-in-a-join – Pogrindis Aug 10 '21 at 11:09
  • It depends on what the type of UnassignUtcDate and UnassignCause is and which of `Equals` and `operator==` it overrides. – Klaus Gütter Aug 10 '21 at 11:10
  • 1
    Does this answer your question? [Checking if an object is null in C#](https://stackoverflow.com/questions/6417902/checking-if-an-object-is-null-in-c-sharp) – Klaus Gütter Aug 10 '21 at 11:12
  • Does this answer your question? [C# difference between == and Equals()](https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals) – Cleptus Aug 10 '21 at 11:13

1 Answers1

3

When using Entity Framework, both 'classic' and Core, operators such as == are recognized in the Expression parse tree. A C# comparison like x.something == null will get translated to x.something IS NULL in the generated SQL.

The same translation is most likely not being done for x.something.Equals(null).


Quoting from docs:

A comparison with the literal null is translated to the appropriate SQL version (is null or is not null).

The document above was written for .NET Framework + LINQ to SQL of that time, but it largely still applies to EF Core. So far I did not find a more up-to-date reference of the same nature.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • Not 100% certain but we could assume, despite the lack of proper tags/information in que question, that the question is Linq to SQL. Very likely this answer adresses the problem. – Cleptus Aug 10 '21 at 11:25
  • 2
    @Cleptus I doubt that LINQ to SQL has `ToListAsync` support. I think we can safely assume the OP is using EF or EF Core. – mjwills Aug 10 '21 at 14:06