Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

DateTime.Now in DataRule Content Filter
BenGreene
#1 Posted : Wednesday, June 08, 2011 4:04:08 AM(UTC)
Rank: Member
Groups: Registered

Joined: 5/24/2011(UTC)
Posts: 21
Location: Melbourne, Australia
I have a content type with a date field, and I would like to filter this to dates in the future in a data rule. Is this possible with the current code? Everything I tried just caused exceptions.

Thanks
jifeng
#2 Posted : Wednesday, June 08, 2011 5:19:32 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
What's the exception?
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
BenGreene
#3 Posted : Wednesday, June 08, 2011 5:27:06 AM(UTC)
Rank: Member
Groups: Registered

Joined: 5/24/2011(UTC)
Posts: 21
Location: Melbourne, Australia
Just a parse exception:

Conversion failed when converting date and/or time from character string.
from Kooboo.CMS.Content.Persistence.SqlServer.QueryProcessor.<List>d__0.MoveNext() +139

To clarify, I was playing around to see if there was a magic string I could put in the filter to get this behavior: 'Now', 'DateTime.Now', etc. There's nothing wrong with the way Kooboo's behaving.

I've written a workaround where I get the top 50 unfiltered results and then .Where(c => c.StartDate > DateTime.Now).Take(5) to get the results I'm after, but I was wondering if there's a 'proper' way to do this?
jifeng
#4 Posted : Wednesday, June 08, 2011 5:31:09 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
yes, it does not support to write C# code in the data rule value.

I think it is a good workagound.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
2 users thanked jifeng for this useful post.
BenGreene on 6/8/2011(UTC), konrad_oblakov on 6/8/2011(UTC)
BenGreene
#5 Posted : Wednesday, June 08, 2011 5:35:10 AM(UTC)
Rank: Member
Groups: Registered

Joined: 5/24/2011(UTC)
Posts: 21
Location: Melbourne, Australia
Thanks for the quick reply.

For anyone else with a similar problem, I had to cast the ViewBag.[DataRule] item to IEnumerable<Kooboo.CMS.Content.Models.ContentBase> as you can't(?) use LINQ directly on a dynamic object.
After that I didn't seem to be able to use the dynamic properties to get to the data, so instead of item.StartDate I had to use item["StartDate"].

Cheers
jifeng
#7 Posted : Wednesday, June 08, 2011 6:07:05 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
Yes, it is the restriction from C# syntax.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
avonwyss
#6 Posted : Wednesday, June 08, 2011 10:14:30 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 11/3/2010(UTC)
Posts: 58
BenGreene;4028 wrote:
For anyone else with a similar problem, I had to cast the ViewBag.[DataRule] item to IEnumerable<Kooboo.CMS.Content.Models.ContentBase> as you can't(?) use LINQ directly on a dynamic object.
After that I didn't seem to be able to use the dynamic properties to get to the data, so instead of item.StartDate I had to use item["StartDate"].

Linq uses extension methods, which aren't real methods, so the dynamic runtime cannot really discover those.

However, you can use "dynamic" for the iterated item instead of "var" to get back the dynamic functionality. Try this:

Code:
foreach (dynamic item in (IEnumerable<ContentBase>)ViewBag.DataRule.YourLinqQuery()) {
    @item.StartDate
}
ella
#8 Posted : Wednesday, November 23, 2011 9:45:07 AM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 57
Location: Russia
Please, explain me again, how I can make filter by date. I try some examples above, but don't understand how it works.
Write example "view".

For example: I have this content type named "Ann":

  • dtStartDate type Date
  • dtEndDate type Date
  • strHeader type String


I have a content folder by this type.
How I can write "view" when I will see only data by filter: (dtStartDate < Today) and (dtEndDate > Today)
jifeng
#9 Posted : Thursday, November 24, 2011 2:01:05 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
ContentHelper.TextFolder("FolderName").CreateQuery().WhereLessThan("dtStartDate", DateTime.Now).WhereGreaterThan("dtStartDate", DateTime.Now)
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
1 user thanked jifeng for this useful post.
ella on 11/24/2011(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF 1.9.5.5 | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.601 seconds.