0

我只想使用 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

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

4

2 回答 2

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; }
于 2014-10-01T11:31:11.743 回答
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
                  };
于 2013-04-28T09:17:56.187 回答