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

Notification

Icon
Error

Select Distinct Field Values
sbrickey
#1 Posted : Friday, December 23, 2011 9:30:35 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 12/16/2011(UTC)
Posts: 35
Location: Ohio, USA
I'm trying to figure out how to select unique values from a specific field.

I'd hoped I could use simple lamda LINQ... but this code failed miserably:
ViewBag.DataRuleContent.Select(i => i.UserId).Distinct()

I could recreate everything with for loops and list<string> constructs... but lamda code has always been cleaner to read.

Is such code possible? what would an example look like?

Thanks,
-Scott
jifeng
#2 Posted : Monday, December 26, 2011 4:33:52 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
Here is the example:

((IEnumerable<TextContent>)(ViewBag.Contents)).Select(it=>it.UserId).Distinct()


I think you can not using the LINQ extension method on the dynamic object.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
sbrickey
#3 Posted : Thursday, December 29, 2011 2:56:28 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 12/16/2011(UTC)
Posts: 35
Location: Ohio, USA
So I was in fact able to use LINQ/Lamda WITH the data rules... just had to do some casting, and treat the data slightly differently...

Turns out, if Paging is enabled in the Data Rules, the object being returned is of type Kooboo.CMS.Sites.DataRule.DataRulePagedList, which implements IEnumerable<T> (allowing LINQ), but also seems to use dynamic objects.

Alternatively, if Paging is NOT enabled, the data rule returns an object of type Kooboo.CMS.Content.Models.ContentBase[].

Good news, is that either way, I could use the fieldname as an index to the object, when dealing with linq... took some casting (since it seems the index returns an 'object'), but I was able to do the following:

((Kooboo.CMS.Sites.DataRule.DataRulePagedList)(ViewBag.BlogPosts)).Where(p => (p["PublishDate"] as DateTime?).HasValue && (p["PublishDate"] as DateTime?).Value <= DateTime.Now)

Since I was also casting the return types, I also had to change the way the properties were referenced in the rest of the cshtml, from using the dynamic object format (obj.property) to the index (obj["property"]).

Not sure if this is better or worse performance than using dynamic objects (since I suspect that they use reflection), and much cleaner than using reflection myself (considered using p.GetType().GetProperty("PublishDate").GetValue(p)).

I'll end up doing a bit of a post mortem on my blog as soon as it starts to go live.
jifeng
#4 Posted : Friday, December 30, 2011 2:02:38 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
Using the dynamic keyword just want to let the code is more clearer and code fluently. I think using the content field as "content.Title" is more acceptable than 'content["Title"]', isn't it?

The dynamic keyword will not take affect the performance, don't worry about it :)
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
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.658 seconds.