public IHttpActionResult GetTraining(int id) { TrainingListM trainingListM = null; try { using (var context = new TrainingAndMeetingDBEntities()) { trainingListM = context.Trainings.Include("TrainingId") //to get list of training by comapring id .Where(s => s.TrainingId == id) .Select(s => new TrainingListM() { Topic = s.Topic, TrainerName = s.User.FirstName, StartTime = s.Schedule.StartTime.Value, EndTime = s.Schedule.EndTime.Value, RoomName = s.Schedule.RoomDetail.RoomName, Description = s.Description }).FirstOrDefault <TrainingListM>(); } } catch (Exception e) { return(BadRequest(e.Message)); } if (trainingListM == null) { return(NotFound()); } return(Ok(trainingListM)); }
public List <Models.TrainingListM> GetTraining() //bind Training list model { List <Models.TrainingListM> list = new List <Models.TrainingListM>(); try { var data = db.Trainings.ToList(); //all list of training foreach (var item in data) { TrainingListM tList = new TrainingListM(); //object of medel class if (item.DeletedAt == null) { tList.TrainingId = item.TrainingId; tList.TrainerName = item.User.FirstName; tList.Topic = item.Topic; tList.Description = item.Description; tList.StartTime = item.Schedule.StartTime.Value; tList.EndTime = item.Schedule.EndTime.Value; tList.RoomName = item.Schedule.RoomDetail.RoomName; list.Add(tList); } } } catch (Exception e) { throw e; } return(list); }