You insert the value of the argument as a constant directly in the
HQL. The NH Linq provider then caches this.
var dateString =
DateTime.Parse(arguments[0].ToString()).ToString("dd-MMM-yyyy");
This isn't very pretty or efficient anyway. If you are going to
extract values from expressions you should cast to the correct
expression type and get the value from there. Besides, why generate a
string in the first place? The datetime should be a parameter typed as
datetime.
Try this instead:
return treeBuilder.Subtract(visitor.Visit(Expression.Property(targetObject,
"Departure")).AsExpression(),
visitor.Visit(arguments[0]).AsExpression());
Since this forwards the original argument expression, the visitor will
recognize it and replace it with a parameter, that will get the
correct value on each invocation.
/Oskar
2012/12/17 Hao Lu <[email protected]>:
> Hi I got some wired issue, just want to ask if anyone could help me.
>
> I got a Customised Linq to Nhibernate Extension for one class method.
>
> public class DateTimeSubstractionInDaysGenerator : BaseHqlGeneratorForMethod
> {
> public DateTimeSubstractionInDaysGenerator()
> {
> var finalisedTourDepartureMethodDefinition =
>
> ReflectionHelper.GetMethodDefinition<FinalisedTourDeparture>(dt =>
> dt.GetDaysToDeparture(DateTime.Now));
>
> SupportedMethods = new[] {
> finalisedTourDepartureMethodDefinition,
> finalisedTourBookingServiceMethodDefinition };
> }
>
> public override HqlTreeNode BuildHql(MethodInfo method, Expression
> targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder
> treeBuilder, IHqlExpressionVisitor visitor)
> {
> var dateString =
> DateTime.Parse(arguments[0].ToString()).ToString("dd-MMM-yyyy");
> return
> treeBuilder.Subtract(visitor.Visit(Expression.Property(targetObject,
> "Departure")).AsExpression(), treeBuilder.MethodCall("to_date",
> visitor.Visit(Expression.Constant(dateString)).AsExpression()));
> }
> }
>
> And I use this class method in my linq query something like this.
> public class FinalisedTourDepartureRepository :
> BaseRepository<FinalisedTourDeparture>,
>
> IFinalisedTourDepartureRepository
> {
> public FinalisedTourDepartureRepository(ISession session) :
> base(session)
> {
> }
>
> #region IFinalisedTourDepartureRepository Members
>
> public IEnumerable<FinalisedTourDeparture>
> GetTourDepartureNeedToBeFinalised(IList<string> transmissionDestinations,
> DateTime finalisationDate)
> {
>
> return
> Table.Where(
> td =>
> td.GetDaysToDeparture(finalisationDate) <= 10);
> }
>
> #endregion
> }
>
> My problem is when I call
> FinalisedTourDepartureRepository.GetTourDepartureNeedToBeFinalised twice in
> a row as follow.
>
> FinalisedTourDepartureRepository.GetTourDepartureNeedToBeFinalised(xxx,
> DateTime.Parse("10/12/2013"))
> FinalisedTourDepartureRepository.GetTourDepartureNeedToBeFinalised(xxx,
> DateTime.Parse("10/12/2014"))
>
> The ReadOnlyCollection<Expression> arguments in method
> DateTimeSubstractionInDaysGenerator.BuildHql always catch the first
> datetime [DateTime.Parse("10/12/2013")], it seems my generator has a cache?
> I don't get it.
>
> Is there anyone can help?
>
> Thanks a lot
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nhusers/-/mGiGpDuJTP8J.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.