HOW TO USE JOIN IN LINQ QUERY MVC .

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 846  View(s)
Ratings:
Rate this:
I want to use Use join in Linq query please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to Use join in Linq query mvc .

    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>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!