如何编写嵌套分组数据的类型 C#

我对数据进行了分组,我想将这些数据与模型一起发送到视图。如何将属性写入视图模型


工作计划类


public class WorkPlan:IEntity

{

    public int WorkPlanId { get; set; }

    public int PlanNumber { get; set; }

    public string Day { get; set; }

    public string Lesson { get; set; }

    public string Description { get; set; }

    public bool IsActive { get; set; }

}

工作计划视图模型


public class WorkPlanViewModel:BaseViewModel

{


    public List<WorkPlan> WorkPlans { get; set; }


    public WorkPlan WorkPlan { get; set; }


}

行动


public IActionResult Index()

{

    WorkPlanViewModel model=new WorkPlanViewModel();

    model.Message=new Message();

    var allWorkPlan = workPlanService.GetAll();


    var groupPlan = allWorkPlan.GroupBy(p => p.PlanNumber)

                    .Select(p => new {PlanNumber=p.Key, Day= p.GroupBy(b => b.Day).ToList() } ).ToList();


    return View(model);

}

groupPlan 是我的分组数据


30秒到达战场
浏览 80回答 1
1回答

慕容708150

您可以创建一个反映该匿名类型的类,并将其作为属性添加到您的视图模型中。控制器/操作代码 - 请注意new GroupPlan以下代码Selectvar vm = new MyViewModel();vm.GroupPlans = allWorkPlan.GroupBy(p => p.PlanNumber)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(p => new GroupPlan { PlanNumber = p.Key, Day = p.GroupBy(b => b.Day).ToList() }).ToList();群组计划类public class GroupPlan{&nbsp; &nbsp; public int PlanNumber { get; set; }&nbsp; &nbsp; public List<IGrouping<string, WorkPlan>> Day { get; set; }}视图模型类public class MyViewModel{&nbsp; &nbsp; public List<GroupPlan> GroupPlans { get; set; }&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP