Im trying to extend LINQ to NHibernate provider for my custom function. The problem that I found is that, whenever I try to access a FK from my entity, it generates a "INNER JOIN" statement, but I want to have a LEFT JOIN, so I have all the "DistributorAnswers" where the "Distributor" FK is null.
Here's the BuildHql:
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var targetNode = visitor.Visit(targetObject).AsExpression();
return treeBuilder.Dot(targetNode, treeBuilder.Ident(nameof(DistributorAnswer.Distributor)));
}
The generated SQL query currently uses an INNER JOIN, and I need to modify it to use a LEFT JOIN instead.
Here's the query output that I found with SQL Server Profiler
EXEC sp_executesql N'select TOP (@p0)
...
from [DistributorAnswer] distributo0_
inner join [Distributor] distributo1_ on distributo0_.Distributor_id=distributo1_.Id
And here's the LINQ that i'm trying to run with my custom function:
Session.Query<DistributorAnswer>()
.Select(x => x.MyCustomMethod())
.Take(10)
.ToList();
The problem I am facing is that I don't know how to modify the treeBuilder.Dot method to generate the desired LEFT JOIN. I have examined the available methods and properties in the HqlTreeBuilder class, but I couldn't find a direct way to specify the join type.
I tried to use treeBuilder.LeftJoin but I cannot find any documentation about this and I really don't understand how it works.
Thank you in advance for your help!