从datetime(实体中的数据类型)单独获取日期部分并以网格显示

时间:2013-04-27 07:34:41

标签: linq

我想只使用linq显示日期 我的编码是

    var xyz = from xy in entity.TrainingDetails
                      join ede in entity.EmployeeDetails
                      on xy.TrainerId equals ede.id
                      join lmn in entity.Topics
                      on xy.TopicId equals lmn.id
                      where (xy.TrainerId == ede.id)
                      where (xy.TopicId == lmn.id)
                      select new
                      {
                          EmployeeId = ede.EmployeeId,
                          Trainer = ede.FirstName + ede.MiddleName + ede.LastName,
                          SINo = xy.Id,
                          TrainingDate = xy.TrainingDate,
                          StartTime = xy.StartTime,
                          EndTime = xy.EndTime
                      };

这是有效的。在我的输出中,日期显示在 12-04-2013 00:00:00 这里我只想要12-04-2013。 我试过了

    TrainingDate = String.Format("{0:MM/dd/yyyy}",xy.TrainingDate),
    TrainingDate = xy.TrainingDate.ToString("dd/MM/yyyy"),
    TrainingDate =xy.TrainingDate.ToString("d", DateTimeFormatInfo.InvariantInfo)
    TrainingDate =xy.TrainingDate .Date

所有这些但仍然没有得到。

2 个答案:

答案 0 :(得分:0)

      var xyz = from xy in entity.TrainingDetails.AsEnumerable()
                  join ede in entity.EmployeeDetails
                  on xy.TrainerId equals ede.id
                  join lmn in entity.Topics
                  on xy.TopicId equals lmn.id
                  where (xy.TrainerId == ede.id)
                  where (xy.TopicId == lmn.id)
                  select new
                  {
                      EmployeeId = ede.EmployeeId,
                      Trainer = ede.FirstName + ede.MiddleName + ede.LastName,
                      SINo = xy.Id,
                      TrainingDate = xy.TrainingDate.ToString("dd/MM/yyyy"),
                      StartTime = xy.StartTime,
                      EndTime = xy.EndTime
                  };

答案 1 :(得分:0)

使用Nullable类型(即DateTime?或DateTimeOffset?)。 •使用静态字段DateTime.MinValue或DateTimeOffset.MinValue(默认值 这些类型的值)

您应该像这样编写代码:

 public IEnumerable<TrainingDetailItem> SelectAll()
    {
          List<TrainingDetailItem> gridRowItemsList = null;

          using (var context = new entities())
          {


                 gridRowItemsList = (from table in context.table
                                     select new GridRowItem
                                     {

                                     }).OrderByDescending(item => item.Id).ToList();
          }

   }

and in models create class TrainingDetailItem and set time like this:

public DateTime? TrainingDate { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTimeDate { get; set; }