NHibernate Overcoming NotSupportedException

Does anyone know of any way to overcome a NotSupportedException? I have a method against the user:

public virtual bool IsAbove(User otherUser) { return HeirarchyString.StartsWith(otherUser.HeirarchyString); } 

And I want to do:

 _session.Query<User>.Where(x => loggedInUser.IsAbove(x)); 

But this throws a NotSupportedException. However, the real pain is that using

 _session.Query<User>.Where(x => loggedInUser.HeirarchyString.StartsWith(x.HeirarchyString)); 

works absolutely fine. However, I don’t like this as a solution because it means that if I changed the IsAbove method, I have to remember all the places where I duplicated the code when I want to update it.

+6
source share
1 answer

Name the specification expression and reuse, for example:

 public Expression<Func<....>> IsAboveSpecification = (...) => ...; public virtual bool IsAbove(User otherUser) { return IsAboveSpecification(HeirarchyString, otherUser.HeirarchyString); } 

Reuse IsAboveSpecification in the request as needed. If the IsAbove () method is used frequently, use can cache the result of the Compile () method in the expression.

+3
source

Source: https://habr.com/ru/post/918341/


All Articles