This might be a very simple issue, but i'm new to Umbraco so just trying to get head around it.
I've managed to create some custom templates which are based off the NewOverview to NewsItem relationship.
In the overview page i'm wanting to select all Item pages and group by a custom field I have set up. I've added the following into my page:
@{
var groupedItems = CurrentPage.Children.GroupBy("Type");
foreach (var group in groupedItems)
{
@group.Key
}
}
However when I run this I get the following error message:
'object' does not contain a definition for 'Key'..
I've used the debug tool in visual studio and can see that @group definitely has the two properties Key which countains my value and Element, but I can't seem to do anything with either. Can some please help?
I had this same issue and just deicded to work around it as I couldn't see what was causing it (like you say the Umbraco.Web.Dynamics.Grouping object clearly has a Key and Elements property so should work). In the end I created a list of the property I wanted to group by and then lopped through that doing a Children.Where() on the property/value.
Another issue I experienced was trying to get that list of property values to only go through distinct valus - even including @using System.Linq the view would always give an error about there being no Distinct() on List- very odd, so I added a "dealwith" list to act as a filter.
It isn't the prettiest solution but it works for grouping content under headers.
@{
List<string> dealtWith = new List<string>();
var categories = CurrentPage.Children().Distinct("category").Pluck("category");
foreach (var category in categories) {
if(dealtWith.Contains(category)) { continue; }
var children = CurrentPage.Children().Where("Category == \""+category+"\"");
<h3>@category (@children.Count())</h3>
foreach (var child in children) {
<h4>@child.Question</h4>
<div>@child.Answer</div>
}
dealtWith.Add(category);
}
}
I'll try implementing this tonight and see how it goes, but it looks good. Was a bit frustrating, especially as no-one else seems to have had the problem.
GroupBy Issue
This might be a very simple issue, but i'm new to Umbraco so just trying to get head around it.
I've managed to create some custom templates which are based off the NewOverview to NewsItem relationship.
In the overview page i'm wanting to select all Item pages and group by a custom field I have set up. I've added the following into my page:
@{
var groupedItems = CurrentPage.Children.GroupBy("Type");
foreach (var group in groupedItems)
{
@group.Key
}
}
However when I run this I get the following error message:
'object' does not contain a definition for 'Key'..
I've used the debug tool in visual studio and can see that @group definitely has the two properties Key which countains my value and Element, but I can't seem to do anything with either. Can some please help?
Can anybody help?
Hi coodey,
I had this same issue and just deicded to work around it as I couldn't see what was causing it (like you say the Umbraco.Web.Dynamics.Grouping object clearly has a Key and Elements property so should work). In the end I created a list of the property I wanted to group by and then lopped through that doing a Children.Where() on the property/value.
Another issue I experienced was trying to get that list of property values to only go through distinct valus - even including @using System.Linq the view would always give an error about there being no Distinct() on List- very odd, so I added a "dealwith" list to act as a filter.
It isn't the prettiest solution but it works for grouping content under headers.
Thanks for the reply Miku.
I'll try implementing this tonight and see how it goes, but it looks good. Was a bit frustrating, especially as no-one else seems to have had the problem.
Cheers
This worked brilliantly. Well done. Cheers
Hi all, I can see this is not too old a thread.
I have done it different, look here:
Kind regards :)
Scott
is working on a reply...