Home > DeveloperSection > Forums > How to use 'let' keyword in linq query mvc entity framework.
Ankit Singh

Total Post:341

Points:2389
Posted on    January-21-2016 9:44 PM

 ASP.NET MVC C#  LINQ  Mvc  Entity Framework 
Ratings:


 1 Reply(s)
 347  View(s)
Rate this:
I want to use 'let' keyword in linq query mvc entity framework. how to do this please help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    January-21-2016 9:47 PM

The 'let' keyword is useful in query syntax. It  allows re-use of the expression and makes the query more readable.

@model IList<ForumMVC.CUSTOMER>
@{
    ViewBag.Title = "WebGrid CRUD Operations";  
}

<table style="background-color: #ccc; width:800px;">
    <thead style="background-color: red;">
        <tr>
            <th>ID</th>
             <th>Name</th>
             <th>Phone</th>
             <th>Email</th>            
        </tr>
    </thead>
    <tbody style="background-color: #ddd;">
        @foreach(var obj in Model)
        {
        <tr>
            <th>@obj.CUST_ID</th>
             <th>@obj.CUST_NAME</th>
             <th>@obj.CUST_PHONE</th>
             <th>@obj.CUST_EMAILID</th>            
        </tr>
        }
    </tbody>
</table>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForumMVC;
using System.Data.Entity;
using ForumMVC.Models;
namespace ForumMVC.Controllers
{
    public class HomeController : Controller
    {
        forumEntities1 db = new forumEntities1();
        public ActionResult Index()
        {         
            IList<CUSTOMER> data = (from s in db.CUSTOMER
                                    let lowercaseCustomerName = s.CUST_NAME.ToLower()
                                    where lowercaseCustomerName.EndsWith("singh")
                                    select s).ToList();
            return View(data);
        }

    }
}


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

Follow MindStick