Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • LINQ Select in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 60
    Comment on it

    The Language Integrated Query is used to perform the task of the user programmer more easier as it can be used to interact with any data source like XML file or SQL Server database or Array or Hash table.

     

    We use it for the selection of the data from the database.

     

    First we will create context for it then we will put the datatable into that context after doing that we will use it for the manipulation with thew database.

     

     

     

     

     

    Then we will create the controller action for which it is being called.

     

     

     [HttpPost]
            public JsonResult Save(EmployeeModel EmpMod)
            {
                new GridModel().Save(EmpMod);
                return Json(true);
            }

     

     

     

    After doing this we will create the method which is used for writing the LINQ query for the database.

     

     

      public List<EmployeeModel> GetPlayers(int? page, int? limit, string sortBy, string direction, string searchString, out int total)
            {
                total = (from u in DataContextObj.EmployeeDetails select u).Count();
                var records = (from p in DataContextObj.EmployeeDetails
                               select new EmployeeModel
                               {
                                   EmpId = p.EmpId,
                                   FirstName = p.EmpFirstName,
                                   MiddleName = p.EmpMiddleName,
                                   LastName = p.EmpLastName
                               }).AsQueryable();
    
                if (!string.IsNullOrWhiteSpace(searchString))
                {
                    records = records.Where(p => p.FirstName.Contains(searchString) || p.LastName.Contains(searchString));
                }
    
                if (!string.IsNullOrEmpty(sortBy) && !string.IsNullOrEmpty(direction))
                {
                    if (direction.Trim().ToLower() == "asc")
                    {
                        records = SortHelper.OrderBy(records, sortBy);
                    }
                    else
                    {
                        records = SortHelper.OrderByDescending(records, sortBy);
                    }
                }
    
                if (page.HasValue && limit.HasValue)
                {
                    int start = (page.Value - 1) * limit.Value;
                    records = records.Skip(start).Take(limit.Value);
                }
    
                return records.ToList();
            }
    

     

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: