Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to SQL question - specifying columns then modifying column
    text
    copied!<p>I'm trying to write a linq to sql method that handles sorting, paging, and filtering for an ajax grid. I created a partial class Employee that has a TotalRecordCount, as I need to pass this to the javascript for setting up the pager. The problem is that it won't build because I can't set the AnonymousType#1.TotalRecordCount, it's read-only. Yet if I do "select new Employee", then it will throw the Exception - "Explicit construction of entity type 'InVision.Data.Employee' in query is not allowed.".</p> <p>Here's the code...</p> <pre><code>public string GetPageJSON(string sortColumn, string sortDirection, int pageNumber, int pageSize, EmployeeSearch search) { var query = from e in db.Employees select new { EmployeeID = e.EmployeeID, FirstName = e.FirstName, LastName = e.LastName, LoginName = e.LoginName, IsLockedOut = e.IsLockedOut, TotalRecordCount = e.TotalRecordCount }; //searching. if (search.FirstName.Length &gt; 0) query = query.Where(e =&gt; e.FirstName.Contains(search.FirstName)); if (search.LastName.Length &gt; 0) query = query.Where(e =&gt; e.LastName.Contains(search.LastName)); if (search.LoginName.Length &gt; 0) query = query.Where(e =&gt; e.LoginName.Contains(search.LoginName)); if (search.Status.Length &gt; 0) query = query.Where(e =&gt; (search.Status == "Active" &amp;&amp; !e.IsLockedOut) || (search.Status == "Inactive" &amp;&amp; e.IsLockedOut)); //sorting. query = query.OrderBy(sortColumn, sortDirection); //get total record count. int totalRecordCount = query.Count(); //paging. query = query.Skip((pageNumber - 1) * pageSize).Take(pageSize); //set total record count. var list = query.ToList(); if (list.Count &gt; 0) { list[0].TotalRecordCount = totalRecordCount; //throws exception } //return json. JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(list); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload