12

I have a simple QueryOver

var q = SessionInstance.QueryOver<Person>().Where(p => p.Number.Equals(number));

Number field type is int. This query has a runtime error by this message:

Unrecognised method call: System.Int32:Boolean Equals(Int32)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ehsan
  • 3,431
  • 8
  • 50
  • 70
  • 1
    can you post the mapping of `Person`? also you can try `.Where(p => p.Number == number` – J. Ed Sep 17 '11 at 13:51
  • Thanks! The Problem resolved by replacement '==' leiu equals. What is different between '==' and 'Equals' in field by int Type? – Ehsan Sep 18 '11 at 02:38

1 Answers1

30

The == operator generates a BinaryExpression which can be converted to SQL and the .Equals() method generates a MethodCallExpression which apparently is not converted to SQL.

Usually the binary operators are handled in QueryOver and also in Linq but only a few method calls are handled (string.Contains, array.Contains, etc.) so you better use operators when possible.

Also remember that the operators/method calls are not actually executed, but converted SQL statements so if you have custom overrides/implementations for them they might not work as expected.

Given the above your code would be rewritten as:

var q = SessionInstance.QueryOver<Person>().Where(p => p.Number == number);
GôTô
  • 7,974
  • 3
  • 32
  • 43
Iulian Margarintescu
  • 2,656
  • 21
  • 22