Voici un HqlGenerator qui résout ce problème :
public class ConcatHqlGenerator : BaseHqlGeneratorForMethod
{
public ConcatHqlGenerator()
: base()
{
this.SupportedMethods = new[]
{ ReflectionHelper.GetMethodDefinition(() => string.Concat(null, null)) };
}
public override HqlTreeNode BuildHql(MethodInfo method,
Expression targetObject,
ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder,
IHqlExpressionVisitor visitor)
{
return treeBuilder.Concat(
new[]
{
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression()
});
}
}
Ajoutez ceci à votre registre HQLGeneratorsRegistry et vous serez prêt à lancer des appels à string.Concat dans vos instructions LINQ.
public class LinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
public LinqToHqlGeneratorsRegistry()
: base()
{
this.Merge(new ConcatHqlGenerator());
}
}
private static ISessionFactory CreateSessionFactory()
{
var configuration = new NHib.Cfg.Configuration();
configuration.Properties.Add(NHibernate.Cfg
.Environment.LinqToHqlGeneratorsRegistry,
typeof(LinqToHqlGeneratorsRegistry).AssemblyQualifiedName);
configuration.Configure();
return configuration.BuildSessionFactory();
}