Comment on page

Comparing Database Objects


Database object can either be compared in SQL WHERE clauses or in programming code.

Comparison in SQL Queries

In SQL queries database objects can be compared either by the equals = operator or by the ObjectNo value. The direct comparison is always preferable.
var products = Db.SQL<Product>(
"SELECT p FROM Product p WHERE p.Customer = ?", customer);
var products = Db.SQL<Product>(
"SELECT p FROM Product p WHERE p.Customer.ObjectNo = ?",

Comparison Between Instances

Two instances of a database class can be compared either with the Object.Equals method or with the ObjectNo value. The Object.Equals method is the preferable way.
var firstProduct = new Product();
var secondProduct = new Product();
var anotherFirstProduct = Db.FromId(firstProduct.GetObjectNo());
firstProduct.Equals(secondProduct); // false
firstProduct.GetObjectNo() == secondProduct.GetObjectNo(); // false
firstProduct.Equals(anotherFirstProduct); // true
firstProduct.GetObjectNo() == anotherFirstProduct.GetObjectNo(); // true
The equals == operator and the Object.ReferenceEquals method will always return false when comparing database objects.