You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Institution实体映射为ETModel列表以优化资源占用?

问题:如何在EF中仅加载ETModel所需列并填充视图模型

我需要仅向用户展示ET相关数据,因此定义了精简的ETModel来避免加载Institution实体的所有列,但在控制器中填充视图模型时遇到了问题——直接用Institution会加载全部列,违背了使用ETModel减少资源消耗的初衷。

相关代码定义

数据库实体Institution

public partial class Institution
{
    public int? Id { get; set; }

    public string? District { get; set; }
    public string? InstitutionCode { get; set; }
    public string? InstitutionName { get; set; }
    public string? DemolitionStatus { get; set; }
    public string? ReinforcementStatus { get; set; }
    public string? BuildingOwnerStatus { get; set; }
    public string? BuildingOwnerInstitution { get; set; }
    public string? ClosureStatus { get; set; }
    public string? ActivityStatus { get; set; }
    public string? ETStatus { get; set; }
    public int? ETPhase1 { get; set; }
    public int? ETPhase2 { get; set; }
    public int? ETPhase3 { get; set; }
    public string? InfrastructureStatus { get; set; }
    public string? InfrastructureScope { get; set; }
    public string? InfrastructureInfo { get; set; }
    public string? InfrastructureScopeOut { get; set; }
    public string? IAccessStatus { get; set; }
    public string? IAccessType { get; set; }
    public string? ComputerClassStatus { get; set; }
    public int? ComputerClassNumber { get; set; }
    public int? PCNumber { get; set; }
    public string? ComputerClassScope { get; set; }
    public int? ETNeed { get; set; }
}

精简模型ETModel

public class ETModel
{
    public int? Id { get; set; }

    public string? District { get; set; }
    public string? InstitutionCode { get; set; }
    public string? InstitutionName { get; set; }
    public string? ActivityStatus { get; set; }
    public string? ETStatus { get; set; }

    public int? ETPhase1 { get; set; }
    public int? ETPhase2 { get; set; }
    public int? ETPhase3 { get; set; }
    public int? ETNeed { get; set; }
}

视图模型ETListVM

public class ETListVM
{
    public List<ETModel> ETyes { get; set; } 
    public List<ETModel> ETnone { get; set;}
}

控制器中的问题代码

public IActionResult Index(string district)
{
     ETListVM vm= new ETListVM();
     var ETyesList = c.**XXX**
                      .Where(p => p.District == district && p.ETStatus == "yes")
                      .ToList();
     var ETnoneList = c.**XXX**
                       .Where(p => p.District == district && p.ETStatus == "none")
                       .ToList();

     vm.ETyes = ETyesList;
     vm.ETnone = ETnoneList;
    
     return View();
}

如果在XXX处填写Institution可以运行,但会加载所有列。我不想重新定义新实体,希望直接使用ETModel来仅加载所需列并填充视图模型。


内容的提问来源于stack exchange,提问作者Ahmet Demir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 06:15:46