asp.net linq到aadata数据表

时间:2015-06-30 03:10:18

标签: asp.net ajax linq datatable

您好我一直在尝试在ASP.net中使用dataTable作为一个项目但我已经进入了一个墙,我想从sql数据库传递到数据库字符串格式,我已经设法获得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);
    }
}

无论如何要进行此转换?我已经被困了一个星期只是试图解决这个问题,我的数据库也包含布尔值和其他数值,我也尝试创建一个字符串列表,但是当我尝试进行转换时它告诉我一些值不是& #39;我的查询中的字符串..

1 个答案:

答案 0 :(得分:0)

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

        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);