linq 查询订单帮助

发布于 2024-11-08 19:54:19 字数 2787 浏览 0 评论 0原文

以下操作方法将 json 数据返回到我的 jqGrid,如本文所述:
使用-jquery-grid-with -asp.net-mvc

我的问题基于此 linq 查询:

    var data = (
    from job in jobs
    select new
    {
      id = job.Id,
      cell = new List<string>() { job.Industry, job.OccupationName }
    }).ToArray();

Why are the cell list items notordered as expected? (行业后跟职业名称):

我期望的是:


{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"IT","OccupationName":"Developer"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}

我得到的是:


{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"Developer","OccupationName":"IT"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}

感谢您的任何建议!

为了清楚起见,整个操作方法:

    public ActionResult OccupationList(string sidx, string sord, int page, int rows)
    {
        using (var context = Service.EF.GetContext())
        {
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.sOccupations.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            sidx = "it.[" + sidx + "]";

            var jobs = context.sOccupations
                .OrderBy(sidx + " " + sord)
                .Skip(pageIndex * pageSize)
                .Take(pageSize);

            var data = (
                  from job in jobs
                  select new
                  {
                      id = job.Id,
                      cell = new List<string>() { job.Industry, job.OccupationName }
                  }).ToArray();

            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = data
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
    }  

我当前的解决方案是经典的 foreach 语句 - 但我仍然想知道如何将其表达为 linq 查询

    IList<JsonRow> data = new List<JsonRow>();
    JsonRow jsonRow;
    foreach (var item in jobs)
    {
        jsonRow = new JsonRow();
        jsonRow.id = item.Id;
        jsonRow.cell = new string[2] { item.Industry, item.OccupationName };
        data.Add(jsonRow);
    }

    class JsonRow
    {
        public Guid id { get; set; }
        public string[] cell { get; set; }
    }

the following action method returns json data to my jqGrid as described in this article:
using-jquery-grid-with-asp.net-mvc

my question is based on this linq query:

    var data = (
    from job in jobs
    select new
    {
      id = job.Id,
      cell = new List<string>() { job.Industry, job.OccupationName }
    }).ToArray();

Why are the cell list entries not ordered as expected? (Industry followed by OccupationName) :

What I expect is:


{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"IT","OccupationName":"Developer"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}

what i get is:


{"total":1400,"page":1,"records":7000,
"rows":[
{"id":"1","cell":{"Industry":"IT","OccupationName":"Administrator"}},
{"id":"2","cell":{"Industry":"Developer","OccupationName":"IT"}},
{"id":"4","cell":{"Industry":"IT","OccupationName":"Programmer"}}
]}

Thanks for any suggestions!

For clarity the whole action method:

    public ActionResult OccupationList(string sidx, string sord, int page, int rows)
    {
        using (var context = Service.EF.GetContext())
        {
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.sOccupations.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            sidx = "it.[" + sidx + "]";

            var jobs = context.sOccupations
                .OrderBy(sidx + " " + sord)
                .Skip(pageIndex * pageSize)
                .Take(pageSize);

            var data = (
                  from job in jobs
                  select new
                  {
                      id = job.Id,
                      cell = new List<string>() { job.Industry, job.OccupationName }
                  }).ToArray();

            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = data
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
    }  

My current solution is a classic foreach statement-
but I'm still wondering how to express this as a linq query

    IList<JsonRow> data = new List<JsonRow>();
    JsonRow jsonRow;
    foreach (var item in jobs)
    {
        jsonRow = new JsonRow();
        jsonRow.id = item.Id;
        jsonRow.cell = new string[2] { item.Industry, item.OccupationName };
        data.Add(jsonRow);
    }

    class JsonRow
    {
        public Guid id { get; set; }
        public string[] cell { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-11-15 19:54:19

您必须根据需要订购“细胞”。

 var data = (
        from job in jobs
orderby job.OccupationName
        select new
        {
          id = job.Id,
          cell = new List<string>() { job.Industry, job.OccupationName }
        }).ToArray();

您必须检查语法,因为我还没有尝试运行它。
编辑:我尝试过这个,它应该有效。

You'd have to order the "cell" as you like.

 var data = (
        from job in jobs
orderby job.OccupationName
        select new
        {
          id = job.Id,
          cell = new List<string>() { job.Industry, job.OccupationName }
        }).ToArray();

You will have to check the syntax, since I haven't tried to run it yet.
Edit: I tried this, it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文