Home > DeveloperSection > Forums > How to Use join in Linq query mvc .
Ankit Singh

Total Post:341

Points:2389
Posted on    November-30-2015 10:32 PM

 .NET LINQ  Bootstrap  Mvc 
Ratings:


 1 Reply(s)
 411  View(s)
Rate this:
I want to use Use join in Linq query please help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    November-30-2015 10:34 PM

LINQ has a JOIN query operator that provides SQL JOIN like behavior and syntax. As you know, Inner join returns only those records or rows that match or exists in both the tables. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Diagnostics;
using ForumMVC;
using System.Threading;
using ForumMVC;
namespace ForumMVC.Controllers
{
    public class HomeController : Controller
    {
        forumEntities  db = new forumEntities();
        public ActionResult Index()
        {
            var result = from t in db.Customer
                         join x in db.Order on t.CustomerID equals x.CustomerID
                         select (new CustomerData {CustomerID=t.CustomerID,CustomerName=t.CustomerName,Amount=x.amount });
                        
            return View(result.ToList());
        }
      
    }
   public  class CustomerData
    {
       public int CustomerID { get; set; }
       public string CustomerName { get; set; }
       public decimal? Amount { get; set; }
    }
}
 @model IList<ForumMVC.Controllers.CustomerData>
 @using ForumMVC.Controllers;
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<table class="table table-hover table-bordered table-condensed table-striped">
    <thead>
      <tr>
        <th>CustomerID</th>
        <th>Customer Name</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
       @foreach(CustomerData obj in Model)
       {
      <tr>
        <td>@obj.CustomerID</td>
        <td>@obj.CustomerName</td>
        <td>@obj.Amount</td>
      </tr>
       }
     
    </tbody>
  </table>

Don't want to miss updates? Please click the below button!

Follow MindStick