0

I would like to use "Equals" to check wether a value in my DB and another given value are the same. Unfortunatly, NHbernate does not seam to support "Equals", so I searched in the internet and found this tutorial: http://www.primordialcode.com/blog/post/nhibernate-3-extending-linq-provider-fix-notsupportedexception
At the end, I have to register the extension - but in which file should I do that?

Edit
Maybe I should describe my problem a bit better...
The situation: I have a enumeration called "GoodsType". Possible values are for example "Iron", "Wood", "Stone", etc. This enum is used in the class "InventoryGoods":

public class InventoryGoods: Goods
{
    public virtual User Owner { get; set; }
    public virtual Guid Id { get; set; }
    public virtual long Amount { get; set; }
    public virtual GoodsType GoodsType { get; set; }
    public virtual float Price { get; set; }
}

If a user want to sell something, I have to check wether he has enough of this specific good. I created a function for this check. This function is able to find the actual Owner by itself (the user which is logged in), so I only have to send the GoodsType and the Amount. But if I use this request: InventoryGoods good = _repository.First(x => (Equals(x.GoodsType, GivenGoodsType)) && (x.Amount >= GivenAmount));, the script stops with the error System.NotSupportedException: Boolean Equals(System.Object, System.Object)

I searched a lot, and somewhere on SO (unfortunatly I don't remember the link) I found the statement that "Equals" is not supported by NHibernate (because it is not translateable to SQL). So I searched for a solution and found the tutorial named above. But I'm unable to find the file where I have to register the extension.

I already found this solution: InventoryGoods good = _repository.First(x => x.Amount >= GivenAmount).Where(x => (Equals(x.GoodsType, GivenGoodsType)));, but I would prefer a way without double-checking via "Where", only with "First".

Edit 2
I'm using Fluent NHibernate for my mapping.

Christopher
  • 2,005
  • 3
  • 24
  • 50

1 Answers1

0

As long as I know, NHibernate supports Equals for object comparison. In fact, you should override it, as long as default equals implementation relays on memory address, not object values.

See this similar questions: Why is it important to override GetHashCode when Equals method is overridden?

NHibernate: Reasons for overriding Equals and GetHashCode

Community
  • 1
  • 1
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • It does not seam to supports "equals" - or I'm doing something wrong. If I try to use it like `return _repository.First(x => x.GoodsType.Equals(GoodsTypeToCheck));` it throws the error `System.NotSupportedException: Boolean Equals(System.Object)` – Christopher May 28 '12 at 16:50
  • What are you trying to do? Get assemblies of certain type? – Oscar May 29 '12 at 18:26
  • Also, notice that the article you reference says that he was used an alpha release of version 3, current GA version is 3.3.0 stable. Are you using latest version? – Oscar May 29 '12 at 18:33
  • I updated my question, I hope, it is clearer now. I think I'm using a new version of NHibernate, but where can I check that? – Christopher May 31 '12 at 18:29
  • In NHibernate's sourceforge page, of course! Check if your NHibernate.dll version is the same as the latest release at: http://sourceforge.net/projects/nhibernate/ – Oscar Jun 01 '12 at 06:59
  • I updated my version - but the error still occurs :-( I'm sorry, but I just saw that I seem to have forgotten to write that I'm using Fluent NHibernate - does this make any difference? – Christopher Jun 01 '12 at 14:47