вот мой контроллер
```
// Controllers/CombinedCostController.cs

using AutoMapper;
using BuildingFlow.Data.Contract;
using BuildingFlow.Data.Entities;
using BuildingFlow.Services.Models.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace BuildingFlow.Web.Controllers
{
  public class CombinedCostController : Controller
  {
    private readonly IUnitOfWork _unitOfWork;
    //private readonly IMapper _mapper;
    private readonly IRepository<Cost> _costItemRepository;
    private readonly IRepository<CostType> _costTypeRepository;
    private readonly IRepository<Unit> _unitRepository;
    private readonly IRepository<Material> _materialRepository;
    private readonly IRepository<Work> _workRepository;

    public CombinedCostController(IUnitOfWork unitOfWork, IMapper mapper)
    {
      _unitOfWork = unitOfWork;
      //_mapper = mapper;
      _costItemRepository = _unitOfWork.GetRepository<Cost>();
      _costTypeRepository = _unitOfWork.GetRepository<CostType>();
      _unitRepository = _unitOfWork.GetRepository<Unit>();
      _materialRepository = _unitOfWork.GetRepository<Material>();
      _workRepository = _unitOfWork.GetRepository<Work>();
    }

    // GET: CombinedCost/Index
    public async Task<IActionResult> Index()
    {
      // Получаем все записи из CostItems для определенного проекта
      var costItems = await _costItemRepository.AsReadOnlyQueryable()
          .Where(ci => ci.ProjectId == 1) // Замените условие по необходимости
          .ToListAsync();

      // Извлекаем необходимые идентификаторы для оптимизации выборок
      var costTypeIds = costItems.Select(ci => ci.CostTypeId).Distinct().ToList();
      var unitIds = costItems.Where(ci => ci.UnitId.HasValue).Select(ci => ci.UnitId.Value).Distinct().ToList();
      var costIds = costItems.Select(ci => ci.CostId).Distinct().ToList();

      // Получаем данные из связанных таблиц
      var costTypes = await _costTypeRepository.AsQueryable()
          .Where(ct => costTypeIds.Contains(ct.CostTypeId))
          .ToListAsync();

      var units = await _unitRepository.AsQueryable()
          .Where(u => unitIds.Contains(u.UnitId))
          .ToListAsync();

      var materials = await _materialRepository.AsQueryable()
          .Where(m => costIds.Contains(m.CostId))
          .ToListAsync();

      var works = await _workRepository.AsQueryable()
          .Where(w => costIds.Contains(w.CostId))
          .ToListAsync();

      // Объединяем данные в ViewModel
      var combinedViewModel = costItems.Select(ci => new CostItemCombinedViewModel()
      {
        // Поля из CostItem
        
        CostId = ci.CostId,
        ProjectId = ci.ProjectId,
        ParentCostId = ci.ParentCostId,
        CostTypeId = ci.CostTypeId,
        CostName = ci.CostName,
        ISRComment = ci.ISRComment,
        ContractorComment = ci.ContractorComment,
        UnitId = ci.UnitId,
        ConsumptionCoefficient = ci.ConsumptionCoefficient,
        BOCount = ci.BOCount,
        TotalCount = ci.TotalCount,
        MatEquipmPrice = ci.MatEquipmPrice,
        SmpPnpPrice = ci.SmpPnpPrice,
        TotalPrice = ci.TotalPrice,
        MatEquipmCost = ci.MatEquipmCost,
        SmpPnpCost = ci.SmpPnpCost,
        TotalCost = ci.TotalCost,
        HierarchyLevel = ci.HierarchyLevel,
        NumberItem = ci.NumberItem,
        NodeCode = ci.NodeCode,

        // Поля из CostType
        CostTypeName = costTypes.FirstOrDefault(ct => ct.CostTypeId == ci.CostTypeId)?.TypeName,

        // Поля из Unit
        UnitName = ci.UnitId.HasValue ? units.FirstOrDefault(u => u.UnitId == ci.UnitId.Value)?.UnitName : null,
        UnitDescription = ci.UnitId.HasValue ? units.FirstOrDefault(u => u.UnitId == ci.UnitId.Value)?.UnitDescription : null,

        // Поля из Material
        MaterialId = materials.FirstOrDefault(m => m.CostId == ci.CostId)?.MaterialId,

        // Поля из Work
        WorkId = works.FirstOrDefault(w => w.CostId == ci.CostId)?.WorkId
      }).ToList();

      var hierarchy = BuildHierarchy(combinedViewModel);

      return View(hierarchy);
    }

    public async Task<IActionResult> IndexT() {
      // Получаем все записи из CostItems для определенного проекта
      var costItems = await _costItemRepository.AsReadOnlyQueryable()
          .Where(ci => ci.ProjectId == 1) // Замените условие по необходимости
          .ToListAsync();

      // Извлекаем необходимые идентификаторы для оптимизации выборок
      var costTypeIds = costItems.Select(ci => ci.CostTypeId).Distinct().ToList();
      var unitIds = costItems.Where(ci => ci.UnitId.HasValue).Select(ci => ci.UnitId.Value).Distinct().ToList();
      var costIds = costItems.Select(ci => ci.CostId).Distinct().ToList();

      // Получаем данные из связанных таблиц
      var costTypes = await _costTypeRepository.AsQueryable()
          .Where(ct => costTypeIds.Contains(ct.CostTypeId))
          .ToListAsync();

      var units = await _unitRepository.AsQueryable()
          .Where(u => unitIds.Contains(u.UnitId))
          .ToListAsync();

      var materials = await _materialRepository.AsQueryable()
          .Where(m => costIds.Contains(m.CostId))
          .ToListAsync();

      var works = await _workRepository.AsQueryable()
          .Where(w => costIds.Contains(w.CostId))
          .ToListAsync();

      // Объединяем данные в ViewModel
      var combinedViewModel = costItems.Select(ci => new CostItemCombinedViewModel() {
        // Поля из CostItem

        CostId = ci.CostId,
        ProjectId = ci.ProjectId,
        ParentCostId = ci.ParentCostId,
        CostTypeId = ci.CostTypeId,
        CostName = ci.CostName,
        ISRComment = ci.ISRComment,
        ContractorComment = ci.ContractorComment,
        UnitId = ci.UnitId,
        ConsumptionCoefficient = ci.ConsumptionCoefficient,
        BOCount = ci.BOCount,
        TotalCount = ci.TotalCount,
        MatEquipmPrice = ci.MatEquipmPrice,
        SmpPnpPrice = ci.SmpPnpPrice,
        TotalPrice = ci.TotalPrice,
        MatEquipmCost = ci.MatEquipmCost,
        SmpPnpCost = ci.SmpPnpCost,
        TotalCost = ci.TotalCost,
        HierarchyLevel = ci.HierarchyLevel,
        NumberItem = ci.NumberItem,
        NodeCode = ci.NodeCode,

        // Поля из CostType
        CostTypeName = costTypes.FirstOrDefault(ct => ct.CostTypeId == ci.CostTypeId)?.TypeName,

        // Поля из Unit
        UnitName = ci.UnitId.HasValue ? units.FirstOrDefault(u => u.UnitId == ci.UnitId.Value)?.UnitName : null,
        UnitDescription = ci.UnitId.HasValue ? units.FirstOrDefault(u => u.UnitId == ci.UnitId.Value)?.UnitDescription : null,

        // Поля из Material
        MaterialId = materials.FirstOrDefault(m => m.CostId == ci.CostId)?.MaterialId,

        // Поля из Work
        WorkId = works.FirstOrDefault(w => w.CostId == ci.CostId)?.WorkId
      }).ToList();

      var hierarchy = BuildHierarchy(combinedViewModel);

      return View(hierarchy);
    }

    private List<CostItemHierarchyViewModel> BuildHierarchy(List<CostItemCombinedViewModel> combinedViewModel)
    {
      var lookup = combinedViewModel.ToLookup(ci => ci.ParentCostId);
      List<CostItemHierarchyViewModel> BuildNodes(int? parentId)
      {
        return lookup[parentId]
          .OrderBy(ci => ci.NumberItem) // Опционально: сортировка по NumberItem
          .Select(ci => new CostItemHierarchyViewModel
          {
            Id = ci.CostId,
            NumberItem = ci.NumberItem,
            CostName = ci.CostName,
            MatEquipmCost = ci.MatEquipmCost,
            SmpPnpCost = ci.SmpPnpCost,
            // Добавьте другие свойства по необходимости
            Children = BuildNodes(ci.CostId)
          })
          .ToList();
      }

      return BuildNodes(null); // Начинаем с корневых элементов (ParentId == null)
    }


  }
}
```
его работа меня полностью устраивает
но у меня изменилась входная модель
```
    public class Work :IEntity
    {
        [Key]
        public int WorkId { get; set; }

        [Required]
        [MaxLength(50)]
        public string NumberItem { get; set; }

        [MaxLength(50)]
        public string? NodeCode { get; set; }

        [Required]
        [MaxLength(200)]
        public string? CostName { get; set; }

        public string? ISRComment { get; set; }

        public string? ContractorComment { get; set; }

        [MaxLength(50)]
        public string? UnitOfMeasure { get; set; }

        public decimal? ConsumptionCoefficient { get; set; }

        public decimal? BOCount { get; set; }

        public decimal? TotalCount { get; set; }

        public decimal? MatEquipmPrice { get; set; }

        public decimal? SmpPnpPrice { get; set; }

        public decimal? TotalPrice { get; set; }

        public decimal? MatEquipmCost { get; set; }

        public decimal? SmpPnpCost { get; set; }

        public decimal? TotalCost { get; set; }

        // Внешние ключи
        [ForeignKey("Project")]
        public int ProjectId { get; set; }

        [ForeignKey("ParentWork")]
        public int? ParentWorkId { get; set; }

        [ForeignKey("Status")]
        public int StatusId { get; set; }

        [ForeignKey("Unit")]
        public int? UnitId { get; set; }

        public int? HierarchyLevel { get; set; }

        public bool IsMigrated { get; set; }

        public DateTime? StartDate { get; set; }

        public DateTime? EndDate { get; set; }

        public string? Comment { get; set; }

        // Навигационные свойства
        public Project Project { get; set; }

        public Work ParentWork { get; set; }

        public Status Status { get; set; }

        public Unit Unit { get; set; }

        public ICollection<Work> SubWorks { get; set; } = new List<Work>();

        public ICollection<WorkMaterial> WorkMaterials { get; set; } = new List<WorkMaterial>();
    }
``` 
мне нужно получить hierarchy на основе новой модели