1
\$\begingroup\$

Continuing on this question on Stack Overflow, I've found a working solution.

The case

The case I'll create is that I've got a lot of persons and a knows person get 0, 1 or multiple languages. The language can get 0, 1 or multiple MVPs. Inside the back office of Umbraco I've created two document types:

  • Person: with the first name, last name, photo and a (Obsolete) Multinode Treepicker for the known programming languages of that person.
  • Language: With the name and a piece of code.

The code

On the template of the language, I'll create a table with the language, the code sample and the MVPs.

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ProgrammingLanguages>
@using EventCalendar.Website.Models;

@{
    Layout = "Master.cshtml";

    var searcher = ExamineManager.Instance.SearchProviderCollection["PersonsSearcher"];
    var searchCriteria = searcher.CreateSearchCriteria();
    var query = searchCriteria.Field("nodeTypeAlias", "person").Compile();
    var persons = searcher.Search(query);
}

<table class="table">
    <tr>
        <th>Name</th>
        <th>Output <samp>Hello World!</samp></th>
        <th>MVP</th>
    </tr>

    @foreach (ProgrammingLanguage lang in Model.Content.Children)
    {
        var res = persons.Where(x => x.Fields["knowledge"] != null && x.Fields["knowledge"].Split(',').Contains(lang.Id.ToString())); @* <-- instead of this *@

        <tr>
            <td>@lang.PageTitle</td>
            <td><pre>@lang.HelloWorld</pre></td>
            <td>
                <ul>
                    @foreach (var r in res)
                    {
                        <li>@r.Fields["firstName"] @r.Fields["lastName"]</li>
                    }
                </ul>
            </td>
        </tr>
    }
</table>

knowledge is a comma separated string with the IDs of the language.

This outputs this:

Output

Good to know

I've created an indexer and a searcher for the persons.

Examine Manager of Umbraco

After building:

Build of PersonsIndexer

My questions

  1. Is this code performance enough?
  2. Can I use typed persons? I'll like to use this code below instead of the marked line.

    var res = persons.Where(x => x.Knowledge != null && x.Knowledge.Contains(lang)));
    

    Remark: I've written .Contains(lang) so I'll get an IEnumerable<Person>!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.