使用从LINQ到Entites的数据填充包含其他ViewModel的ViewModel

时间:2016-05-06 12:41:40

标签: c# asp.net-mvc-5 linq-to-entities

我刚刚完成了我的第一个复合体,我认为它很复杂,ViewModels。它包含嵌套的LINQ to Entities,我很难弄清楚如何使用public class TimesheetWeeklyTableVM { public int TimesheetHeaderID { get; set; } //public DateTime weekEndingDate { get; set; } public decimal TotalWeekHours { get; set; } public List<TimesheetDailyVM> DaysOfWeek { get; set; } public TimesheetWeeklyTableVM(DateTime weekEndingDate) { DaysOfWeek = new List<TimesheetDailyVM>(); for (var i = 6; i >= 0; i--) { DaysOfWeek.Add(new TimesheetDailyVM() { CurrentDate = weekEndingDate.AddDays(-i) }); } } } public class TimesheetDailyVM { public DateTime CurrentDate { get; set; } public double TotalHours { get { return TimeSpan.FromSeconds(Tasks.Sum(p => (p.StartDateTime - p.EndDateTime).TotalSeconds)).TotalHours; } } public IEnumerable<TimesheetDailyHoursVM> Tasks { get; set; } public TimesheetDailyVM() { Tasks = new List<TimesheetDailyHoursVM>(); } } public class TimesheetDailyHoursVM { public int TimesheetID { get; set; } public DateTime StartDateTime { get; set; } public DateTime EndDateTime { get; set; } public string ProjectCode { get; set; } public string ProjectDescription { get; set; } public string TaskCode { get; set; } public string TaskDescription { get; set; } } 用数据填充整个图表。我怎么能填满整个图表?

我的ViewMode:

public class TimesheetHeader
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public int EmployeeID { get; set; }
    public DateTime WeekEndingDate { get; set; }
    public decimal TotalHours { get; set; }
    public int CurrentStatusID { get; set; }

    public decimal OtherPay { get; set; }
    public int? ApprovedByID { get; set; }
    public DateTime? DateSubmitted { get; set; }
    public DateTime? DateApproved { get; set; }

    [Column(TypeName = "varchar(MAX)")]
    public string ManagerNotes { get; set; }

    [ForeignKey("EmployeeID")]
    public Employee Employee { get; set; }

    [ForeignKey("ApprovedByID")]
    public Employee ApprovedBy { get; set; }

    [ForeignKey("CurrentStatusID")]
    public StatusOption CurrentStatus { get; set; }
}

public class Timesheet
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public int TimesheetHeaderID { get; set; }
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }
    public string ProjectCode { get; set; }
    public string TaskCode { get; set; }

    [Column(TypeName = "varchar(MAX)")]
    public string WorkDescription { get; set; }

    [ForeignKey("TimesheetHeaderID")]
    public TimesheetHeader TimesheetHeader { get; set; }
}

我将把数据提取到上面的实体:

            var weekEndingDate = TimesheetHelper.GetWeekEndingDate(date);

            using (var db = new JobSightDbContext())
            {
                var model = db.TimesheetHeaders
                              .Where(timesheet => timesheet.WeekEndingDate == weekEndingDate)
                              .Select(timesheet => new TimesheetWeeklyTableVM(weekEndingDate)
                              {
                                  TimesheetHeaderID = timesheet.ID,
                                  TotalWeekHours = timesheet.TotalHours
                              }).FirstOrDefault();

                model.DaysOfWeek = db.Timesheets
                                     .Where(timesheet => timesheet.TimesheetHeaderID == model.TimesheetHeaderID)
                                     .OrderBy(timesheet => timesheet.StartDateTime)
                                     .Select(timesheet => new TimesheetDailyVM()
                                     {   
                                         Tasks.TimesheetID = timesheet.ID,
                                         StartDateTime = timesheet.StartDateTime,
                                         EndDateTime = timesheet.EndDateTime,
                                         ProjectCode = timesheet.ProjectCode,
                                         TaskCode = timesheet.TaskCode
                                     }).ToList();

                return PartialView(model);
            }

我到目前为止填写的代码:

TimesheetDailyHoursVM

此代码向我提供了错误&#34;名称&#39;任务&#39;在当前上下文中不存在。无效的初始化成员声明&#34;和&#34; TimesheeDailyVM不包含&#39; XXX&#39;&#34;

的定义

如果我试着加入sudo apt-get install php7.0-gd ,那么我会得到一个隐含的转换错误。

1 个答案:

答案 0 :(得分:1)

假设您要显示一周中每一天的记录,您需要按日期对Timesheet条记录进行分组,并将每个相应的组添加到相应日期的关联Tasks属性中< / p>

var timeRecords = db.Timesheets
    .Where(x => x.TimesheetHeaderID == model.TimesheetHeaderID)
    .Groupby(x => x.StartDateTime);

foreach(var group in timeRecords)
{
    var index = // see notes below
    model.DaysOfWeek[index].Tasks = group.Select(x => new TimesheetDailyHoursVM()
    {
        TimesheetID = x.ID,
        StartDateTime = x.StartDateTime,
        ....
    });
}

在上文中,index需要基于group.Key StartDateTime。通常这将是

index = group.Key.DayOfWeek;

星期日为0,星期一为1等,但在您的代码中使用

for (var i = 6; i >= 0; i--)
{
    DaysOfWeek.Add(new TimesheetDailyVM()
    {
        CurrentDate = weekEndingDate.AddDays(-i)
    });
}

并且不清楚第一天是否实际上是星期天