Equals() is not supported by MongoQuery (?!)

62 views
Skip to first unread message

Tim

unread,
Jul 21, 2010, 4:39:28 AM7/21/10
to mongodb-csharp, craigg...@gmail.com
Hello, here is another problem I would like to discuss.

I'm trying to get item from mongodb by key. The key is Oid.
the expression actually comes from upper level of application:

x => x.Service.Key.Equals(service.Key) &&
x.Resource.Key.Equals(resource.Key)

when executed got error: "the method Equals is not supported"

I believe that it can be implemented the way like ExpressionType.Equal
it has.
What do you think?

P.S.
== can't be used here in my case.
x => x.Service.Key==service.Key && x.Resource.Key==resource.Key




Steve Wagner

unread,
Jul 21, 2010, 5:51:03 AM7/21/10
to mongodb...@googlegroups.com
Hi Tim, why == cant be used here?

The use of equals would be assume that the Equals function is called on
each object, but that dose not happen. This expression is transformed to
a database query, so the database can not know how equals is implemented.

-Steve

Tim

unread,
Jul 21, 2010, 8:00:40 AM7/21/10
to mongodb-csharp
The problem is in the == operator.

From MSDN:
"For reference types other than string, == returns true if its two
operands refer to the same object"

Let say we have 2 Oid objects that have equal content (just for
example 4c46d7657c1a601774000003) but have different references. It is
possible, right?

Oid o1 = Oid.NewOid();
Oid o2 = new Oid(o1);

Console.WriteLine(ReferenceEquals(o1, o2).ToString()); // False
Console.WriteLine(o1.Equals(o2).ToString()); // True
Console.WriteLine((o1==o2).ToString()); // True

Object x1 = o1;
Object x2 = o2;

Console.WriteLine(x1.Equals(x2).ToString()); // True
Console.WriteLine((x1 == x2).ToString()); // False <--- It's
here !!!!!!! operator is static, can't be virtual.

I have IKey interface that have System.Object as a Key property.
So Oid is actually stored as System.Object . Splitting data storage
from business logic (some explanations about it are at pic
http://i30.tinypic.com/2hx3vo5.png)

And the problem is that when we call operator == for Object it doesn't
call overloaded == from Oid, so we have false as a result.

As result I have to use
.Where(x=>x.Key.Equals(y.Key))
instead of
.Where(x=>x.Key==y.Key)
Anywhere, and often it goes to lower layers, to storage driver.

yes, it's a problem now.
Any thoughts ?


craigg...@gmail.com

unread,
Jul 21, 2010, 8:04:05 AM7/21/10
to Tim, craigg...@gmail.com, mongodb-csharp
Yes, we just need to support object.Equals. Could you add this to our jira?

jira.mongodb.com, C# Driver.

Tim

unread,
Jul 21, 2010, 8:16:04 AM7/21/10
to mongodb-csharp
good solution.

I think, it should be working.

i will add it.

Tim

unread,
Jul 21, 2010, 8:24:01 AM7/21/10
to mongodb-csharp
Reply all
Reply to author
Forward
0 new messages