@inject IList<IMenuItem> Menu
@inject IHttpContextAccessor HttpContextAccessor
@inject IDataContextFactory<SeniorDataContext> dbFactory

@{
    var session = HttpContextAccessor.HttpContext.Session;
    string username = session.GetString("username");

    if (username != null)
    {
        Menu = new List<IMenuItem>(Menu);
        Menu.RemoveAt(1);
        Menu.Insert(1, new MenuItem("Dati personali", "/user/" + username));
        if (session.GetString("role").Equals("doctor"))
        {
            var db = dbFactory.Create();
            var patients = (from p in db.Patients
                            where p.Doctor.Equals(username)
                            join u in db.Users on p.Username equals u.Username
                            select new { Username = p.Username, Name = u.Name + " " + u.LastName }).ToArray();
            var sub = new SubMenu() { Text = "Pazienti", Items = new List<MenuItem>() };
            foreach (var p in patients)
            {
                sub.Items.Add(new MenuItem(p.Name, "/user/" + p.Username));
            }
            Menu.Add(sub);
        }
    }
}
<ul class="sidebar-menu" data-widget="tree">
    @foreach (var menuItem in Menu)
    {
        switch (menuItem)
        {
            case MenuItem single:
                <li>
                    <a href="@single.HRef">@single.Text</a>
                </li>
                break;
            case SubMenu multi:
                <li class="treeview">
                    <a href="#">
                        <i class="fa fa-link"></i><span>@multi.Text</span>
                        <span class="pull-right-container">
                            <i class="fa fa-angle-left pull-right"></i>
                        </span>
                    </a>
                    <ul class="treeview-menu">
                        @foreach (MenuItem item in multi.Items)
                        {
                            <li>
                                <a href="@item.HRef">@item.Text</a>
                            </li>
                        }
                    </ul>
                </li>
                break;
        }
    }
</ul>