Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Greg Jenson 24 posts 157 karma points
    Dec 01, 2020 @ 23:13
    Greg  Jenson
    0

    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.

    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 &ndash; @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.

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Dec 02, 2020 @ 12:30
    Steve Morgan
    100

    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?

    enter image description here

    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.

  • Greg Jenson 24 posts 157 karma points
    Dec 02, 2020 @ 22:44
    Greg  Jenson
    0

    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.

  • Greg Jenson 24 posts 157 karma points
    Dec 10, 2020 @ 22:00
    Greg  Jenson
    1

    Finally got around to testing this - results are exactly what I was after. Thank you!

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Dec 11, 2020 @ 08:03
    Steve Morgan
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft