Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I am using DataTables on my MVC to perform server side processing for searching fields from the column on my table column from the database. On my Controller class an exception is thrown at
//  This throws at here on this line. System.Linq.Dyanmic.ExpressionParse.ParsePrimaryStart();


What I have tried:

C#
<pre>
       [HttpPost]
       public ActionResult GetList()
        {
            //Server side Parameter.
            int start = Convert.ToInt32(Request["start"]);
            int length = Convert.ToInt32(Request["length"]);
            string searchValue = Request["search[value]"];
            string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"];
            string sortDirection = Request["order[0] [dir]"];

            List<TblEventsManagements> empList = new List<TblEventsManagements>();
            using (eNtsaOnlineRegistrationDBContext db = new eNtsaOnlineRegistrationDBContext())
            {
                 empList = db.TblEventsManagements.ToList<TblEventsManagements>();
                int totalrows = empList.Count;

                if (!string.IsNullOrEmpty(searchValue))
                {
                    empList = empList.Where(x => x.TrainingType.ToLower().Contains(searchValue.ToLower()) || x.TrainingDescription.ToLower().Contains(searchValue.ToLower()) || x.Price.ToString().Contains(searchValue.ToLower())
                    || x.Venue.ToLower().Contains(searchValue.ToLower()) || x.Facilitator.ToLower().Contains(searchValue.ToLower()) || x.WhoAttend.ToLower().Contains(searchValue.ToLower()) || x.Rsvp.ToLower().Contains(searchValue.ToLower())).ToList<TblEventsManagements>();


                }
                    int totalrowsafterfiltering = empList.Count;

                    empList = empList.OrderBy(sortColumnName + " " + sortDirection).ToList<TblEventsManagements>(); // This line thrown the above error

                    empList = empList.Skip(start).Take(length).ToList<TblEventsManagements>();

                    
                
                return Json(new { data = empList, draw = Request["draw"], recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering }, JsonRequestBehavior.AllowGet);
            }


                
        }


<script>

      $(document).ready(function () {

             $("#EventManagementTable").DataTable({
              "ajax": {
                  "url": "/Dashboard/GetList",
                  "type": "POST",
                  "datatype":"json"
              },
                 "columns": [
                  {"data": "TrainingType", "name": "TrainingType"},
                  { "data": "TrainingDescription", "name": "TrainingDescription" },
                  { "data": "Price", "name": "Price" },
                  { "data": "Venue", "name": "Venue" },
                  { "data": "Facilitator", "name": "Facilitator" },
                  { "data": "WhoAttend", "name": "WhoAttend" },
                  {"data": "RSVP", "name": "RSVP"},
              ],

              "serverSide": "true",
              "order":[0,"asc"],
              "processing": "true",
              "language": {
                  "processing":"processing... please wait"
              }



          });

      });
Posted
Updated 11-Feb-20 21:26pm
v2
Comments
Richard Deeming 11-Feb-20 9:16am    
Debug your code and examine the sortColumnName and sortDirection variables to make sure they contain the values you expect.

NB: You seem to have an extra space in the sortDirection line: it should be Request["order[0][dir]"], not Request["order[0] [dir]"].

You're also far too keen with converting the data to a list. Ignoring the initial new List<TblEventsManagements>() assignment, which is immediately thrown away, you call .ToList four times. That will load the entire database table into memory, then copy it around multiple times before returning the result. Remove all but the last .ToList call, and all of the .ToLower calls in your Where clause, and you should get better performance:
IQueryable<TblEventsManagements> empList = db.TblEventsManagements;
int totalrows = empList.Count();
int totalrowsafterfiltering = totalrows;

if (!string.IsNullOrEmpty(searchValue))
{
    empList = empList.Where(x => x.TrainingType.Contains(searchValue) || ... || x.Rsvp.Contains(searchValue));
    totalrowsafterfiltering = empList.Count();
}

empList = empList.OrderBy(sortColumnName + " " + sortDirection).Skip(start).Take(length);

return Json(new { data = empList.ToList(), draw = ... }, JsonRequestBehavior.AllowGet);
phil.o 11-Feb-20 10:12am    
A virtual 5 :)
gcogco10 12-Feb-20 1:24am    
Richard, i am now getting this error 'No property or field 'asc' exists in type 'TblEventsManagements''
Richard Deeming 12-Feb-20 8:14am    
Which dynamic LINQ library are you using?
gcogco10 12-Feb-20 3:26am    
I dont know, i thought by now by mapping this fields from Json format on Ajax should be reference. Now i am getting no field "TrainingTypeasc' not exist. How can i fix this issue? Please help mates

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900