开发者

Nhibernate/Linq: NHibernate.QueryException : could not resolve property: Profile.class of: MyNamespace.MyObject

I'm having an issue with my linq query. I am trying to filter objects based on selected values. We use a query model which returns a System.Linq.Expressions.Expression and uses it to create an nhibernate query. Here is my linq expression.

x =>(request.InitialLoad
     || (!request.InitialLoad
         && (Enum.GetValues(typeof(MyType)).Length == request.MyTypes.Length
             ||
             (Enum.GetValues(typeof(MyType)).Length != request.MyTypes.Length
              &&
              ((request.MyTypes.Contains((int)MyType.Referrals)
                && x.Post.PostType == StatusPostType.Referral)
               ||
               (request.MyTypes.Contains((int)MyType.Businesses)
  开发者_运维知识库              && x.Post.Profile is BusinessProfile)
               ||
               (request.MyTypes.Contains((int)MyType.Members) 
                && x.Post.Profile is UserProfile)
              )
             )
            )
        )
    )
    && x.Profile.Equals(request.Profile);

The mappings (we are using fluent) Look like this:

MyObject (Post property):

References(x => x.Post, "PostId");

MyObject.Post (Profile property):

References(x => x.Profile, "ProfileId");

When I change x.Post.Profile is SomeType to x.Post.Profile.GetType() == typeof(SomeType) it throws a different error, which is

System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

When I take out the type comparison boolean expressions and only leave in the Referrals expression, it works fine for filtering only on that one option.

The properties are not modified in any way by the model. They are virtual properties with the default get/set.

Any ideas?


I'm pretty sure NHibernate.Linq does not support filtering on class type directly. If you need to distinguish between the types, I would use a property value on the base class (probably an enum) that is set to the proper value in the child classes. You can then perform your comparison like this:

x.Post.Profile.Type = ProfileTypes.BusinessProfile

Just set this property statically in the constructor for the child classes, map it with NHibernate, and set update=false for the property mapping. Although somewhat inelegant, this should give you the results you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜