获取日期部分使用LINQ

时间:2015-07-31 07:33:58

标签: linq entity-framework

我有四个表格,DatestartingDateEndingDate字段的数据类型为ApplyingDate
我正在使用ADO.net实体框架。

我已经编写了以下查询来获取结果,但我得到的是日期和时间,我只想要日期部分。
我使用了EntityFunctions.TuncateTime,但我的结果仍然相同。

有人可以建议我,如何只获取日期?

var leaveList = (from application in db.tbl_ApplicationData
     join employee in db.tbl_EmployeeDetails
     on application.UserName equals employee.UserName
     join leaveType in db.tbl_LeaveType
     on application.LeaveTypeId equals leaveType.LeaveTypeId
     join status in db.tbl_Status
     on application.ApplicationStatusId equals status.StatusId
     where application.UserName == "100083"
     select new
     {
         EmployeeName = employee.EmployeeName,
         LeaveTypeID = leaveType.LeaveTypeName,
         StartingDate = EntityFunctions.TruncateTime(application.StartingDate),
         EndingDate =  EntityFunctions.TruncateTime(application.EndingDate),
         AppliedDate = EntityFunctions.TruncateTime(application.ApplyingDate),
         NoOfDays = application.NoOfDays,
         LeavePurpose = application.LeavePurpose,
         LeaveStatus = status.StatusName
     });

1 个答案:

答案 0 :(得分:2)

如果您的实体模型是System.DateTime,则可以在使用对象时使用DateTime方法:

select new {
    EndingDate =  application.EndingDate
};

var myValue = leaveList.EndingDate.Date;

或者如果你想要一个字符串:

leaveList.EndingDate.ToShortDateString()