我有SQL查询,然后转换这个LINQ,然后在jquery中绑定gridview。现在,SQL查询中有一列没有名称。
正如你在"("无列名)"的结果中看到的那样,现在我想给这个列起个名字,以及我如何在代码和jquery 中添加这个列
我有类似于这个的sql查询
ALTER procedure [dbo].[grid_data]
@fromdate datetime,
@todate datetime,
@region varchar(50)
as
Select D.ID as
ID,
(Select Count(*) from tblve WHERE MID = D.ID and Vme <> '') as
total,
D.lim,
D.Speed
from tblRe M
inner join tblReg D
On M.RID = D.RID
这个返回数据就像这个
ID (No column name) lim Speed
19235 3 1065 120
19655 5 923 120
20261 4 757 120
19659 4 551 122
更新
然后我把它转换成linq,就像这样。。现在的问题是,当我添加这一列"总计"时,当我按下f12时,控制台显示"http://localhost:33578/WebForm1.aspx/search_data Failed to load resource: the server responded with a status of 500 (Internal Server Error)"
[WebMethod]
public static string search_data(DateTime fromdate, DateTime todate, string region)
{
try
{
T1 ts = new T1();
var query = (
from M in ts.tblRe
join D in ts.tblReg on M.RID equals D.RID
where
M.Region == region
&& M.StartDate <= fromdate
&& M.EndDate >= todate
select new {
D.ID,
total= ts.tblve.Where(x => x.MID == D.ID && x.Vme !=
'').Count()
D.lim,
D.Speed
}).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("total", typeof(int));
dt.Columns.Add("lim", typeof(string));
dt.Columns.Add("Speed", typeof(string));
foreach (var c in dq)
{
dt.Rows.Add(c.ID,c.total, c.lim, c.Speed);
}
result = DataSetToJSON(dt);
return result;
}
catch (Exception)
{
throw new Exception("");
}
}
然后我在webmethod和jquery中添加列并绑定gridview,就像这个一样
success: function (result) {
var final = JSON.parse(result.d).response;
console.log(JSON.parse(result.d).response)
$("#tabledata").empty();
if (final.length > 0) {
$("#tabledata").append("<tr><th>ID</th><th>total</th><th>lim</th><th>Speed</th></tr>");
for (var i = 0; i < final.length; i++) {
if (final[i] !== null) {
$("#tabledata").append("<tr><td>" +
final[i][0] + "</td> <td>" +
final[i][1] + "</td> <td>" +
final[i][2] + "</td> <td>" +
final[i][3] + "</td></tr>");
}
}
}
}
@EdPlunkett解决方案适用于我的
dt.Columns.Add("计数",typeof(int));