---
title: "How to join linq query in ASP.NET MVC"  
description: "How to join linq query in ASP.NET MVC"  
author: "Manoj Bhatt"  
published: 2016-03-28  
updated: 2016-03-28  
canonical: https://www.mindstick.com/forum/34088/how-to-join-linq-query-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["c#", "asp.net", "asp.net mvc", "entity framework"]  
reading_time: 3 minutes  

---

# How to join linq query in ASP.NET MVC

Hi Everyone,I am working with MVC, C# I have two [model](https://yourviews.mindstick.com/view/81334/america-is-reopening-after-corona-lockdown-but-on-swedish-model) classes, Trainer and Course. I want a [method to return](https://www.mindstick.com/forum/55125/how-to-create-generic-method-to-return-multiple-list-in-c-sharp) information of both models, I created [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) with [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results) 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](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc):**\

```
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](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution.Thank you.

## Replies

### Reply by Anupam Mishra

Hi Manoj,\
You need to create a viewModel to get and set their value:\

```
 public class CourseTaughtByTrainer {       public string CourseName {get;set;}       public string FirstName {get;set}       public string LastName {get;set;} }
```

and return that like:\

```
public List<CourseTaughtByTrainer> CourseByTrainer(){    var temp = (from ar in db.Trainer                join al in db.Course on ar.TrainerID equals al.TrainerID                 select new CourseTaughtByTrainer                {                    CourseName =al.CourseName,                    FirstName = ar.FirstName,                     LastName  = ar.LastName                 });    return temp;}
```


---

Original Source: https://www.mindstick.com/forum/34088/how-to-join-linq-query-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
