Pass Expression as a parameter into a Generic method, and plug the Expression into a CreateCriteria?
I have a generic method that exists in EntityRepository that gets entities by Name, which is defined as follows:
public IEnumerable<T> GetEntitiesByName<T>(string searchExpression)
where T : class, ISearchableEntity, new()
{
return _session.CreateCriteria<T>()
.Add(LambdaSubquery.Property<Fund>(x => x.Id)
.AddNameSearchCriteria<T>(searchExpression)
.List<T>();
}
There are a number of services that use this method, and everything was fine.. but I now have a requirement that some services require more specific search capabilites than is available in this method.
So I'm wonder if it's possible for me to pass in a DetachedCriteria as an Expression (or something) that I could plug into the query? Each service that needs different functionality could pass in the expression it needs to.
I'm not hugely familiar with NHibernate or Expressions, so I m开发者_开发问答ight be out of my depth here. I'm mostly trying to get a feel for what's possible. Otherwise I'll have to abandon the generic method in favour of more specific Repository methods for each service. Thanks.
I posted a code sample in my answer to a somewhat similar question which might help you. However, it is only a rough beginning. If you really want to translate full LINQ expressions into NHibernate and have some semblance of completeness, I would recommend to search for existing solutions first. A quick Google search found me this:
- Implementing LINQ for NHibernate
精彩评论