I've been fighting this for a few days, and need to see if anyone has any ideas where to go with this. The code below is straight from the documentation, and works perfectly.
var faculty = Model.Content.Site().FirstChild("personnel").Children("personnelItem")
.Where(x => x.IsVisible());
@foreach(var item in faculty) {
foreach(var i in item.GetPropertyValue<IEnumerable<string>>("programList"))
{
<li>@i – @item.Name</li>
}
}
Sample result:
Welding Technology – Bob
Advanced Emergency Medical Technician – Steve
Emergency Medical Technician – Steve
Firefighter – Steve
Emergency Medical Technician – Aaron
Nail Technician – Andrea
Practical Nurse – Annie
Academic Development – Sarah
Academic Development – Brent
What I need is to be able to run GroupBy on the checkbox list values (programList). so that 'Emergency Medical Technician' is Key, with the different instructors under listed below.
I think the code below is where I need to be headed, but I can't seem to sort out how to plug my 'var faculty' into the 'List
List<string> colors = new List<string>();
colors.Add("green");
colors.Add("blue");
colors.Add("yellow");
colors.Add("green");
colors.Add("yellow");
IEnumerable<IGrouping<string, List<string>>> groupedColors =
colors.GroupBy(
c => c,
(key, result) => return new { Color = key, Count = result.Count() }
);
foreach(var group in groupedColors)
{
Console.WriteLine(string.Format("Key (Color): {0}tCount: {1}", group.Color, group.Count));
}
Anyone else attempted to do this, with success? Thanks.
I've tried to follow you along.. hopefully I've got this right. You have a list of personnel umbraco items and this contains a checkbox with the faculties?
I realise I've done this in v8 - I've tried to tweak my code accordingly but try this:
var facultiesWithUsers = new Dictionary<string, List<string>>();
var personnel = Model.Content.Site().FirstChild("personnel").Children("personnelItem")
.Where(x => x.IsVisible());
// var personnel = Model.Root().FirstChild("personnel").Children("personnelItem")
// .Where(x => x.IsVisible());
var uniqueFaculties = personnel.Select(x => x.Value<IEnumerable<string>>("faculty")).SelectMany(x => x).Distinct().ToList();
// populate the dictionary
foreach (var uniqueFaculty in uniqueFaculties)
{
var personnelInFacultyList = personnel.Where(x => x.GetPropertyValueValue<IEnumerable<string>>("faculty").ToList().Contains(uniqueFaculty)).Select(x => x.Name).ToList();
//var personnelInFacultyList = personnel.Where(x => x.Value<IEnumerable<string>>("faculty").ToList().Contains(uniqueFaculty)).Select(x => x.Name).ToList();
// add item to the dictionary with the personnel
facultiesWithUsers.Add(uniqueFaculty, personnelInFacultyList);
}
// test output
foreach (var curFacultiesWithUsers in facultiesWithUsers.OrderBy(x => x.Key))
{
<h3>@curFacultiesWithUsers.Key</h3>
<ul>
@foreach(var curUser in curFacultiesWithUsers.Value)
{
<li>@curUser</li>
}
</ul>
}
I tried to do it purely in Linq in a single query and my head hurt - hopefully this will work,
Interested to see if someone else can do it more succinctly.
Steve - thank you. Initial tests look promising, but it seems some of my data is incomplete. Once I get that sorted out, hopefully I'll be able mark as the answer.
GroupBy on Checkbox List property
I've been fighting this for a few days, and need to see if anyone has any ideas where to go with this. The code below is straight from the documentation, and works perfectly.
Sample result:
What I need is to be able to run GroupBy on the checkbox list values (programList). so that 'Emergency Medical Technician' is Key, with the different instructors under listed below.
I think the code below is where I need to be headed, but I can't seem to sort out how to plug my 'var faculty' into the 'List
Anyone else attempted to do this, with success? Thanks.
Hi,
I've tried to follow you along.. hopefully I've got this right. You have a list of personnel umbraco items and this contains a checkbox with the faculties?
I realise I've done this in v8 - I've tried to tweak my code accordingly but try this:
I tried to do it purely in Linq in a single query and my head hurt - hopefully this will work,
Interested to see if someone else can do it more succinctly.
Steve - thank you. Initial tests look promising, but it seems some of my data is incomplete. Once I get that sorted out, hopefully I'll be able mark as the answer.
Finally got around to testing this - results are exactly what I was after. Thank you!
Great stuff,
If it's doing hundreds of nodes and a lot of searching to find these nodes I'd recommend you put it in a partial view and cache it.
is working on a reply...