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?