Getting Exception when calling ObjectId.ToString()

Hi,

I’m getting the error

MongoDB.Driver.Linq.ExpressionNotSupportedException: “Expression not supported: p.Id.ToString().”

when trying to call the ToString method on a ObjectId.

This happens when using UpdateOnAsync with the following filter expression:

var filter = Builders.Filter.Eq(p => p.Id.ToString(), viewModel.Id);

I’m curios why this happens. Shouldn’t it be easily possible to convert a ObjectId to a string object?

Thanks

Sven

1 Like

I solved the problem by changing the filter:

var filter = Builders.Filter.Where(p => p.Id.ToString() == viewModel.Id);

But i’m not sure if it’s the best way