I am working with MVC, C# I have two model classes, Trainer and Course. I want a method to return information of both models, I created already the database with two tables Trainer and Course joined by TrainerID. I want to join both table to assign Course to trainer. How I can do this?
Model Classes:
public class Trainer
{
public int TrainerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public virtual List<Course> Courses { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string CourseName { get; set; }
public virtual Trainer Trainer{ get; set; }
public int TrainerID{ get; set; }
}
Controller:
public class opp
{
MyDbContext db = new MyDbContext();
public List<Course> CourseTaughtByTrainer()
{
var temp = (from ar in db.Trainer
join al in db.Course on ar.TrainerID equals al.TrainerID
select new { al.CourseName , ar.FirstName, ar.LastName });
return temp;
}
}
Can anyone give me a solution.
Thank you.
Anupam Mishra
28-Mar-2016