问题
I would like to be able to use custom string querying within my NHibernate Linq expressions. Let's say for example (and this is just an example) I would like to be able to select entities containing a property which is an anagram of a particular string:
var myEntities = EntityRepository.AllEntities.Where(x => x.Description.IsAnagramOf('hits');
I imagine the steps involved in this process would be:
Define a SQL Server UDF to determine whether two strings are anagrams.
Define an extension method called IsAnagramOf() for the String class.
- (And this is the tricky one). Modify Linq to NHibernate's component for parsing expression trees so that it converts calls to the extension method into the appropriate SQL UDF call.
My question is this. Does Linq to NHibernate contain some kind of extensibility model enabling me to 'slot in' my own custom string operations, or would I literally have to modify the existing source code to add in my shiznit to the expression tree parsing component?
回答1:
The extensibility is built in NH 3.0 (final release next month).
You can see a full working example at http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html
来源:https://stackoverflow.com/questions/4284694/linq-to-nhibernate-extensibility-for-custom-string-query-operations