我正在尝试编写一个linq to sql方法来处理ajax网格的排序,分页和过滤。我创建了一个具有TotalRecordCount的部分类Employee,因为我需要将其传递给javascript来设置寻呼机。问题是它不会构建,因为我无法设置AnonymousType#1.TotalRecordCount,它是只读的。然而,如果我“选择新的员工”,那么它将抛出异常 - “不允许在查询中明确构造实体类型'InVision.Data.Employee'。”。
这是代码......
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 > 0) query = query.Where(e => e.FirstName.Contains(search.FirstName));
if (search.LastName.Length > 0) query = query.Where(e => e.LastName.Contains(search.LastName));
if (search.LoginName.Length > 0) query = query.Where(e => e.LoginName.Contains(search.LoginName));
if (search.Status.Length > 0) query = query.Where(e => (search.Status == "Active" && !e.IsLockedOut)
|| (search.Status == "Inactive" && 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 > 0)
{
list[0].TotalRecordCount = totalRecordCount; //throws exception
}
//return json.
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(list);
}
答案 0 :(得分:1)
您需要选择原始对象,而不是将它们映射到新对象(无论是相同类型还是匿名类型)。
替换它:
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
};
有了这个:
var query = db.Employees.AsQueryable();
然后稍后更换:
var list = query.ToList();
if (list.Count > 0)
{
list[0].TotalRecordCount = totalRecordCount;
}
有了这个:
var list = from e in query
select new
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsActive = !e.IsLockedOut,
TotalRecordCount = totalRecordCount
};
我认为这应该是一切。如果JavaScriptSerializer
需要List
,请确保您按照以下方式使用return serializer.Serialize(list.ToList());
答案 1 :(得分:1)
我最终使用自定义视图类来实现此功能......
partial class EmployeeView
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string LoginName { get; set; }
public bool IsActive { get; set; }
public int TotalRecordCount { get; set; }
}
public string GetPageJSON(string sortColumn, string sortDirection, int pageNumber, int pageSize, EmployeeSearch search)
{
var query = from e in db.Employees
select new EmployeeView
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
LoginName = e.LoginName,
IsActive = !e.IsLockedOut,
TotalRecordCount = 0
};
//searching.
if (search.FirstName.Length > 0) query = query.Where(e => e.FirstName.Contains(search.FirstName));
if (search.LastName.Length > 0) query = query.Where(e => e.LastName.Contains(search.LastName));
if (search.LoginName.Length > 0) query = query.Where(e => e.LoginName.Contains(search.LoginName));
if (search.Status.Length > 0) query = query.Where(e => (search.Status == "Active" && e.IsActive)
|| (search.Status == "Inactive" && !e.IsActive));
//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 > 0)
{
list[0].TotalRecordCount = totalRecordCount;
}
//return json.
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(list);
}