【问题标题】:asp.net linq to aadata datatableasp.net linq 到 aadata 数据表
【发布时间】:2015-09-16 16:42:57
【问题描述】:

您好,我一直在尝试将 dataTable 用于 ASP.net 中的一个项目,但我遇到了一堵墙,我想在其中从 sql 数据库传递到 aadata 字符串格式,我设法获得了 LINQ 查询,它是以下:

     var querytable4 = from table1 in db.ScrapClassifications
                      join table2 in db.ScrapGroups on table1.idGroup equals table2.idGroup
                      join table3 in db.ProdAreas on table1.idArea equals table3.idArea
                      join table4 in db.ScrapSubGroups on table1.idSubGroup equals table4.idSubGroup
                      select new {ActiveBool = table1.Active,
                                   idScrap= table1.idScrapClassification,
                                   Area= table3.Area,
                                   groupDesc= table2.Description,
                                   subgroupdesc=table4.Description,
                                   description=table1.Description
                                    };

它似乎可以正确显示所有值,然后使用 Newtonsoft 序列化程序我尝试执行以下操作:

        var json = JsonConvert.SerializeObject(querytable4);

它会抛出以下输出:

  [{\"ActiveBool\":true,\"idScrap\":1,\"Area\":\"Controllers\",\"groupDesc\":...

我的 ajaxhandler 如下:

    return Json(new
    {
        sEcho = param.sEcho,
        iTotalRecords = 100,
        iTotalDisplayRecords = 100,
        aaData = json

    },JsonRequestBehavior.AllowGet);

只接受以下格式:

public class HomeController : Controller
{
    public ActionResult AjaxHandler(jQueryDataTableParamModel param)
    {
        return Json(new{
                sEcho = param.sEcho,
                iTotalRecords = 97,
                iTotalDisplayRecords = 3,
                aaData = new List<string[]>() {
                    new string[] {"1", "Microsoft", "Redmond", "USA"},
                    new string[] {"2", "Google", "Mountain View", "USA"},
                    new string[] {"3", "Gowi", "Pancevo", "Serbia"}
                    }
            },
        JsonRequestBehavior.AllowGet);
    }
}

无论如何要进行这种转换?我被困了一个星期只是想弄清楚这一点,我的数据库也包含布尔值和其他数值,我也尝试创建一个字符串列表,但是当我尝试进行转换时,它告诉我一些值是我的查询中没有字符串..

【问题讨论】:

    标签: asp.net ajax linq datatable


    【解决方案1】:

    我能够通过在查询中进行以下更改来解决该问题:

            var querytable = (from table1 in db.ScrapClassifications
                              join table2 in db.ScrapGroups on table1.idGroup equals table2.idGroup
                              join table3 in db.ProdAreas on table1.idArea equals table3.idArea
                              join table4 in db.ScrapSubGroups on table1.idSubGroup equals table4.idSubGroup
                              select new{Active=table1.Active,
                                           Classification=table1.idScrapClassification,
                                           Area=table3.Area,
                                           Group=table2.Description,
                                           SubGroup=table4.Description,
                                           Description=table1.Description
                                            }).ToArray();
    

    然后我将 json 输入文件更改为以下内容:

            var data = displayquery.Select(d => (new string[] { d.Active.ToString(), d.Classification.ToString(),d.Area.ToString(),d.Group,d.SubGroup,d.Description  }));
    

    我的 Json 如下所示:

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = querytable.Count(),
                iTotalDisplayRecords = filteredquery.Count(),
                aaData = data 
            },
           "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2012-08-28
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多