Sortable JqGrid using LINQ to MySQL (DbLinq) and Dynamic LINQ - Orderby doesn#39;t work(使用 LINQ to MySQL (DbLinq) 和动态 LINQ 的可排序 JqGrid - Orderby 不起作用)
问题描述
我在 JqGrid 中对条目进行排序时遇到问题.Orderby 似乎不起作用.我在代码中设置了断点,我注意到 orderby 不会改变元素的顺序.知道有什么问题吗?
I've got problem with sorting entries in JqGrid. Orderby seem to not work. I set breakpoint in code and I noticed, that orderby doesn't change order of elements. Any idea what could be wrong?
我正在使用 LINQ to SQL 和 MySQL(DbLinq 项目).
I'm using LINQ to SQL with MySQL (DbLinq project).
我的操作代码:
public ActionResult All(string sidx, string sord, int page, int rows)
{
var tickets = ZTRepository.GetAllTickets().OrderBy(sidx + " " + sord).ToList();
var rowdata = (
from ticket in tickets
select new {
i = ticket.ID,
cell = new String[] {
ticket.ID.ToString(), ticket.Hardware, ticket.Issue, ticket.IssueDetails, ticket.RequestedBy, ticket.AssignedTo, ticket.Priority.ToString(), ticket.State
}
}).ToArray();
var jsonData = new
{
total = 1, // we'll implement later
page = page,
records = tickets.Count(),
rows = rowdata
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
推荐答案
试试下面的
public ActionResult All(string sidx, string sord, int page, int rows)
{
IQueryable<Ticket> repository = ZTRepository.GetAllTickets();
int totalRecords = repository.Count();
// first sorting the data as IQueryable<Ticket> without converting ToList()
IQueryable<Ticket> orderdData = repository;
System.Reflection.PropertyInfo propertyInfo =
typeof(Ticket).GetProperty (sidx);
if (propertyInfo != null) {
orderdData = String.Compare(sord,"desc",StringComparison.Ordinal) == 0 ?
(from x in repository
orderby propertyInfo.GetValue (x, null) descending
select x) :
(from x in repository
orderby propertyInfo.GetValue (x, null)
select x);
}
// if you use fields instead of properties, then one can modify the code above
// to the following
// System.Reflection.FieldInfo fieldInfo =
// typeof(Ticket).GetField (sidx);
// if (fieldInfo != null) {
// orderdData = String.Compare(sord,"desc",StringComparison.Ordinal) == 0 ?
// (from x in repository
// orderby fieldInfo.GetValue (x, null) descending
// select x) :
// (from x in repository
// orderby fieldInfo.GetValue (x, null)
// select x);
/
本文标题为:使用 LINQ to MySQL (DbLinq) 和动态 LINQ 的可排序 JqGrid - Orderby 不起作用
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01