![](/img/trans.png)
[英]Linq to sql .Dynamic Where: string with System.Linq.Dynamic.ParseException: No property or field '0' exists in type 'myTable'
[英]How to fix this issue, System.Linq.Dynamic.ParseException: No property or field asc exist in type 'TblEventsManagements'?
在此处输入图像描述我有这个控制器 GetList 并且我正在使用 Linq.Dynamic 来过滤要搜索的表中的字段,这使用 dataTable 进行服务器端处理。 谁能帮我解决这个问题? 以下是我抛出此错误的逻辑和行;
[\[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\]"\];
using (eNtsaOnlineRegistrationDBContext db = new eNtsaOnlineRegistrationDBContext())
{
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.TrainingDescription.Contains(searchValue) || x.Price.ToString().Contains(searchValue.ToLower())
|| x.Venue.Contains(searchValue) || x.Facilitator.Contains(searchValue) || x.WhoAttend.Contains(searchValue) || x.Rsvp.Contains(searchValue));
}
empList = empList.OrderBy(sortColumnName + "" + sortDirection).Skip(start).Take(length);
return Json(new { data = empList, draw = Request\["draw"\], recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering }, JsonRequestBehavior.AllowGet);
}
}][1]
我忘了打我的 Ajax 电话,我一开始以为问题是我的表中缺少字段。 现在我得到的字段或类型“TrainingTypeasc”不存在,它在我的数据库表上确实存在。 我在哪里可以改进这个逻辑伙伴? 请帮忙。
<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"
}
});
});
替换这个:
empList = empList.OrderBy(sortColumnName + "" + sortDirection).Skip(start).Take(length);
有了这个:
empList = empList.OrderByPropertyName(sortColumnName,sortDirection).Skip(start).Take(length)
并将其添加到您的项目中:
using System.Collections.Generic;
using System.Linq;
namespace "YourNameSpace"
{
public static class ListExtension
{
public static IOrderedEnumerable<T> OrderByPropertyName<T>(this ICollection<T> list, string sortColumnName, string sortDirection)
{
var type = typeof(T);
var property = sortColumnName == "" ? type.GetProperties().FirstOrDefault() : type.GetProperties().Where(p => p.Name == sortColumnName).FirstOrDefault();
return sortColumnName == "asc" ? list.OrderBy(p => property.GetValue(p)) : list.OrderByDescending(p => property.GetValue(p));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.