Can someone tell me why this does not work??: var date = DateTime.Now.AddDays(7).Date;
var collection = @stuff.Where("startDate< "+@date);
While this does:
var collection = @stuff.Where("startDate< DateTime.Now.AddDays(7).Date");
I mean, it's a pain to write the whole thing when have multiple dates... if I add .Day or .Month to both dates it will work, but of course .Date totally fails so how do I get @date to the right format the get this query to recognise it?
Looks like you've worked it out yourself, it doesn't work when you are adding it to the string because it becomes a string rather than a DateTime object.
You could do something along these lines to keep it fully typed and not involve any strings:
var collection = new DynamicNode(-1).Descendants().Where(x => x.GetPropertyValue("startDate").AsDateTime() < DateTime.Now.AddDays(7) && x.GetPropertyValue("startDate") != null);
foreach (dynamic page in collection)
{
<p>@page.Name</p>
}
Thanks :) So it's not able to convert the string to a date in the query then? Like DateTime.Parse()?
I was looking for ways to pass the string because I was retrieving collections for the "current week" and "current month", with the possibility that I would need to pass custom dates through web requests later.
e.g. var diff = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));
var events_this_week = @events.Where("Visible && startDate>=DateTime.Now.AddDays("+@diff+").Date && startDate<=DateTime.Now.AddDays("+@diff+").AddDays(7).Date").OrderBy("startDate");
Finally got around to trying doing a node collection this way while doing a search filter... took me a while to realise needed to cast that my parent node as a "new DynamicNode(@parentNode.Id)" for anything to even start working!
Which is to say, it gave me an error: "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type". A good thing to note for future reference :)
What is the datatype of a node collection anyway? Because I would like to create an empty collection that would return 0 on Count() without cheating with Take(0)...
Date ranges in .Where() queries? (v4.11)
Can someone tell me why this does not work??:
var date = DateTime.Now.AddDays(7).Date;
var collection = @stuff.Where("startDate< "+@date);
While this does:
var collection = @stuff.Where("startDate< DateTime.Now.AddDays(7).Date");
I mean, it's a pain to write the whole thing when have multiple dates... if I add .Day or .Month to both dates it will work, but of course .Date totally fails so how do I get @date to the right format the get this query to recognise it?
This also works: @stuff.Where("startDate < DateTime("[email protected]+","[email protected]+","[email protected]+").Date");
Which I guess is applicable for what I want to do, but is there no better way?
Hi Tatiana,
Looks like you've worked it out yourself, it doesn't work when you are adding it to the string because it becomes a string rather than a DateTime object.
You could do something along these lines to keep it fully typed and not involve any strings:
Jeavon
Thanks :)
So it's not able to convert the string to a date in the query then? Like DateTime.Parse()?
I was looking for ways to pass the string because I was retrieving collections for the "current week" and "current month", with the possibility that I would need to pass custom dates through web requests later.
e.g.
var diff = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));
var events_this_week = @events.Where("Visible && startDate>=DateTime.Now.AddDays("+@diff+").Date && startDate<=DateTime.Now.AddDays("+@diff+").AddDays(7).Date").OrderBy("startDate");
Yes you can, it would be something like this (1011, is the node id of the parent of your events):
Finally got around to trying doing a node collection this way while doing a search filter... took me a while to realise needed to cast that my parent node as a "new DynamicNode(@parentNode.Id)" for anything to even start working!
Which is to say, it gave me an error: "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type". A good thing to note for future reference :)
What is the datatype of a node collection anyway? Because I would like to create an empty collection that would return 0 on Count() without cheating with Take(0)...
Yes you can't use a lambda expression with dynamics which is why you need to use new DynamcNode
I think what you are looking for is DynamicNodeList
is working on a reply...