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();
}
0 Comment(s)