Given the following NHibernate model:

    public class test
    {
        public virtual int id { get; set; }
        public virtual string[] items { get; set; }
    }

which maps to the following PostgreSQL table:

    CREATE TABLE test
    (
        id serial,
        items text[],
        primary key(id)
    )

via the Fluent NHibernate mapping:

    class testMapping : ClassMap<test>
    {
        public testMapping()
        {
            Id(x => x.id);
            Map(x => x.items)
                .CustomType<PostgreSqlStringArrayType>()
                .CustomSqlType("text[]");
        }
    }

where PostgreSqlStringArrayType is a custom UserType and is defined as:

    [Serializable]
    public class PostgreSqlStringArrayType : IUserType
    {
        public SqlType[] SqlTypes
        {
            get
            {
                return new[]
                {
                    new SqlType(DbType.Object)
                };
            }
        }


        public Type ReturnedType
        {
            get { return typeof(string[]); }
        }


        public bool IsMutable { get; private set; }


        public object Assemble(object cached, object owner)
        {
            return cached;
        }


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


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


        public int GetHashCode(object x)
        {
            return (x == null) ? 0 : x.GetHashCode();
        }
        
        public object NullSafeGet(IDataReader rs, string[] names, object 
owner)
        {
            int index = rs.GetOrdinal(names[0]);
            return rs.GetValue(index) as string[];
        }
        
        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var parameter = ((NpgsqlParameter) cmd.Parameters[index]);
            parameter.NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Text;
            parameter.Value = value;
        }


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


        bool IUserType.Equals(object x, object y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }
            return (x != null) && x.Equals(y);
        }
    }


*How do I select only those rows where any element in items matches any 
element in a C# string[] array (named here filterItems)?*


My first thought was to try the following:

    var tests = session.Query<test>
        .Where(t => t.items.Any(item => filterItems.Contains(item)))
        .ToList();

but this fails with the following exception:

    NHibernate.PropertyAccessException was unhandled
      HResult=-2146232832
      Message=Exception occurred getter of IntersectionTest.Models.test.id
      Source=NHibernate
      StackTrace:
           at NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(
Object target)
           at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(
Object entity)
           at NHibernate.Persister.Entity.AbstractEntityPersister.
GetIdentifier(Object obj, EntityMode entityMode)
           at NHibernate.Persister.Entity.AbstractEntityPersister.
IsTransient(Object entity, ISessionImplementor session)
           at NHibernate.Engine.ForeignKeys.IsTransient(String entityName, 
Object entity, Nullable`1 assumed, ISessionImplementor session)
           at 
NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String 
entityName, Object entity, ISessionImplementor session)
           at NHibernate.Type.EntityType.GetIdentifier(Object value, 
ISessionImplementor session)
           at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, 
Object value, Int32 index, ISessionImplementor session)
           at NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand 
command, IList`1 multiSqlQueryParametersList, Int32 
singleSqlParametersOffset, IList`1 sqlQueryParametersList, QueryParameters 
queryParameters, ISessionImplementor session)
           at NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand 
command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, 
ISessionImplementor session)
           at NHibernate.SqlCommand.SqlCommandImpl.Bind(IDbCommand command, 
ISessionImplementor session)
           at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters 
queryParameters, Boolean scroll, ISessionImplementor session)
           at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, 
QueryParameters queryParameters, Boolean returnProxies, IResultTransformer 
forcedResultTransformer)
           at NHibernate.Loader.Loader.
DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, 
QueryParameters queryParameters, Boolean returnProxies, IResultTransformer 
forcedResultTransformer)
           at NHibernate.Loader.Loader.DoList(ISessionImplementor session, 
QueryParameters queryParameters, IResultTransformer forcedResultTransformer)
           at NHibernate.Loader.Loader.DoList(ISessionImplementor session, 
QueryParameters queryParameters)
           at NHibernate.Loader.Loader.ListIgnoreQueryCache(
ISessionImplementor session, QueryParameters queryParameters)
           at NHibernate.Loader.Loader.List(ISessionImplementor session, 
QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
           at NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor 
session, QueryParameters queryParameters)
           at 
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor 
session, QueryParameters queryParameters)
           at 
NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters 
queryParameters, ISessionImplementor session, IList results)
           at NHibernate.Impl.SessionImpl.List(IQueryExpression 
queryExpression, QueryParameters queryParameters, IList results)
           at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression 
queryExpression, QueryParameters parameters)
           at NHibernate.Impl.AbstractQueryImpl2.List()
           at 
NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression 
nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
           at NHibernate.Linq.DefaultQueryProvider.Execute(Expression 
expression)
           at 
NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
           at Remotion.Linq.QueryableBase`1.GetEnumerator()
           at System.Collections.Generic.List`1..ctor(IEnumerable`1 
collection)
           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
           ...
      InnerException: System.Reflection.TargetException
           HResult=-2146232829
           Message=Object does not match target type.
           Source=mscorlib
           StackTrace:
                at 
System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
                at 
System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, 
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo 
culture)
                at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, 
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo 
culture)
                at System.Reflection.RuntimePropertyInfo.GetValue(Object 
obj, Object[] index)
                at 
NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target)

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to