I have entity User which contains property Status of type
DictionaryItem and this is mapped as a UserType. Now i want to make
the following linq statement:

Session.Query<User>().Where(x => x.LoginStatus.Code == "some
code").ToList();

I got following exception:

    Additional information: could not resolve property: Code of: User

I know the problem is that I search using my custom type(property Code
existed on my DictionaryItem user type). nhibernate session query
generates SQL statement when i call ToList(), but LoginStatus is not
Reference type, only User type, is any workaround to make query on
user type?

Source code:
 public class User
{
    public virtual Guid Id { get; set; }
    public virtual DictionaryItem LoginStatus { get; set; }

    public User()
    {
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x =>
x.LoginStatus).CustomType<DictionaryItemCustomType>();
    }
}
public class DictionaryItem
    {
        public virtual int Id { get; set; }
        public virtual string Code { get; set; }
        public virtual string Description { get; set; }
    }
 public class DictionaryItemCustomType : IUserType
    {
        public new bool Equals(object x, object y)
        {
            if (x == null && y == null)
            {
                return true;
            }
            if (x == null || y == null)
            {
                return false;
            }
            return ((DictionaryItem)x).Id == ((DictionaryItem)y).Id;
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object NullSafeGet(IDataReader rs, string[] names,
object owner)
        {
            object value = NHibernateUtil.Int32.NullSafeGet(rs,
names);
            if (value == null)
            {
                return null;
            }

            return
AutofacServiceHostFactory.Container.Resolve<IDictionaryRepository>().DictionaryItems.First(x
=> x.Id == (int)value);
        }

        public void NullSafeSet(IDbCommand cmd, object value, int
index)
        {
            DictionaryItem dictionaryItem = value as DictionaryItem;
            NHibernateUtil.Int32.NullSafeSet(cmd, dictionaryItem ==
null ? (object)null : dictionaryItem.Id, index);
        }

        public object DeepCopy(object value)
        {
            return value;
        }

        public object Replace(object original, object target, object
owner)
        {
            return DeepCopy(original);
        }

        public object Assemble(object cached, object owner)
        {
            throw new NotImplementedException();
        }

        public object Disassemble(object value)
        {
            throw new NotImplementedException();
        }

        public SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.Int32.SqlType }; }
        }

        public Type ReturnedType
        {
            get { return typeof(DictionaryItem); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
    }

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to