一周中的日子正在显示背单词(周六,周五,通过,结婚等),我无法弄清楚为什么

时间:2016-05-11 19:27:10

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

在我的项目中,我通过ViewModel向数据LINQ to Entities填充数据,然后使用该数据构建一个表格,该表格将显示用户的每日时间表。我似乎正在抓取数据,因为当我打印那天工作的小时数时它们是正确的但是问题是打印的日期是错误的,它们正在向后显示。

enter image description here

以下是模型:

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()
    {
        DaysOfWeek = new List<TimesheetDailyVM>();

        foreach (DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek)))
        {
            DaysOfWeek.Add(new TimesheetDailyVM()
            {
                DayOfWeek = dayOfWeek
            });
        }
    }
}

public class TimesheetDailyVM
{
    public DayOfWeek DayOfWeek { get; set; }

    public double TotalHours
    {
        get
        {
            return TimeSpan.FromSeconds(Tasks.Sum(p => (p.EndDateTime - p.StartDateTime).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; }
}

以及用于填充该模型的代码:

var weekEndingDate = TimesheetHelper.GetWeekEndingDate(date);
var employeeID = UserHelper.GetUserID(User.Identity.Name);

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

    var headerID = model.TimesheetHeaderID;

    var timeRecords = db.Timesheets
                        .Where(timesheet => timesheet.TimesheetHeaderID == headerID)
                        .ToList()
                        .GroupBy(timesheet => timesheet.StartDateTime.DayOfWeek);

    foreach (var record in timeRecords)
    {
        model.DaysOfWeek[(int)record.Key].Tasks = record.Select(x => new TimesheetDailyHoursVM()
        {
            TimesheetID = x.ID,
            StartDateTime = x.StartDateTime,
            EndDateTime = x.EndDateTime,
            ProjectCode = x.ProjectCode,
            TaskCode = x.TaskCode
        });
    }

    return PartialView(model);
}

以及显示它的代码:

@model TimesheetWeeklyTableVM

<table border="1">
    <thead>
        <tr>
            <th>
                Total Hours: <br />
                @Model.TotalWeekHours
            </th>
            @foreach (var day in Model.DaysOfWeek)
            {
                <th>
                    @Model.WeekEndingDate.AddDays(-(int)day.DayOfWeek).ToString("ddd MM/dd/yy")
                    <br />@day.TotalHours Hours Worked
                </th>
            }
        </tr>
    </thead>

    <tbody>
    </tbody>
</table>

我尝试在填充.OrderBy(timesheet => timesheet.StartDateTime.DayOfWeek);的{​​{1}}查询中添加LINQ,但这并不影响任何内容。我缺少什么,因此表格会显示timeRecords中的日期?

3 个答案:

答案 0 :(得分:2)

这取决于您订购的DaysOfWeek系列的订单。根据{{​​1}}或high to low值,将打印日期。

low to high

根据当前输出,您的DaysOfWeek列表按升序排序。将其更改为降序,您将获得所需的结果。

@Model.WeekEndingDate.AddDays(-(int)day.DayOfWeek).ToString("ddd MM/dd/yy")

更新 - 您的实体的设计未正确对齐。要修复同步问题,请不要在 @foreach (var day in Model.DaysOfWeek.Cast<int>().OrderByDescending(x => x)) 构造函数中初始化DaysOfWeek列表。可以从TimesheetWeeklyTableVM类属性StartDateTime获取DayOfWeek。只需TimesheetDailyHoursVM即可获得星期几。  假设您的StartDateTime和EndDateTime涵盖一天。 StartDateTime.DayOfWeek的{​​{1}}属性可以初始化为:

DayOfWeek

答案 1 :(得分:1)

看起来您只需要订购"ShortcutName","ShortcutFull","ShortcutPath","Target" "WinSystem.lnk","C:\Users\frode\desktop\test\WinSystem.lnk","C:\Users\frode\desktop\test","C:\Windows\System" "MyEnd.lnk","\\127.0.0.1\c$\EFI\MyEnd.lnk","\\127.0.0.1\c$\EFI","C:\Users\frode\Desktop\TheEnd" "EFI.lnk","C:\Users\frode\desktop\EFI.lnk","C:\Users\frode\desktop","\\127.0.0.1\c$\EFI" "Lol.lnk","C:\Users\frode\desktop\Lol.lnk","C:\Users\frode\desktop","C:\Windows\System32\notepad.exe" 您可以在自己的视图中DaysOfWeek或在此处进行了Model.DaysOfWeek

之后对其进行订购

答案 2 :(得分:1)

问题是你的

@Model.WeekEndingDate.AddDays(-(int)day.DayOfWeek).ToString("ddd MM/dd/yy")

视图中的代码行。您有一个DayOfWeek属性范围从0(星期日)到6(星期六)并假设WeekEndingDate是5月14日(本周最后一天)的集合,那么第一次迭代转换为

 WeekEndingDate.AddDays(-0) // results in 14th May

,第二次迭代转换为

WeekEndingDate.AddDays(-1) // results in 13th May

等。您需要将视图代码更改为

WeekEndingDate.AddDays(((int)day.DayOfWeek) - 6).ToString("ddd MM/dd/yy");

以便第一次迭代转换为

WeekEndingDate.AddDays(-6) // results in 8th May

以便第二次迭代转换为

WeekEndingDate.AddDays(-5) // results in 9th May

作为旁注:我会考虑在TimesheetWeeklyTableVM构造函数

中添加一个参数
public TimesheetWeeklyTableVM(DateTime weekEnding)
{
    DaysOfWeek = new List<TimesheetDailyVM>();
    DateTime startDate = weekEnding.AddDays(-6);
    for(int i = 0; i < 6; i++)
    {
        DaysOfWeek.Add(new TimesheetDailyVM()
        {
            Date = startDate.AddDays(i)
        };
    }
}

并在TimesheetDailyVM模型中,将public DayOfWeek DayOfWeek { get; set; }替换为

[DisplayFormat(DataFormatString = "{0:ddd MM/dd/yy}")]
public DateTime Date{ get; set; }

并在您的第一个查询中,使用

.Select(timesheetHeader => new TimesheetWeeklyTableVM(weekEndingDate)

然后在视图中,您可以使用

@foreach (var day in Model.DaysOfWeek)
{
    <th>
       @Html.DisplayFor(m => day.Date)
       <br />@day.TotalHours Hours Worked
    </th>
}

您还可以为小时数添加DisplayFormat属性

[DisplayFormat(DataFormatString = "{0:0} HoursWorked")] // adjust if you want decimal places
public double TotalHours
{
   .....

并在视图中使用@Html.DisplayFor(m => day.TotalHours)