我有动态创建Json这个奇怪的问题....由于某种原因,这不起作用
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), "<a href=\""+Url.Action("Edit",new{id=company.Id})+"\">"+company.Name.ToString()+"</a>" }
}).ToArray()
};
它给了我一个奇怪的“无法将表达式.......转换成sql”异常
但是通过这个微小的改变它可以正常工作
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), "<a href=\""+Url.Action("Edit",new{id=5})+"\">"+company.Name.ToString()+"</a>" }
}).ToArray()
};
请注意,更改只是使id = 5而不是动态。
此外,这种方法也正常,但我不喜欢它。
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), "<a href=\"Edit/"+company.Id+"\">"+company.Name.ToString()+"</a>" }
}).ToArray()
};
答案 0 :(得分:1)
我不确定这是否会解决您的问题,但假设companies
是来自DataContext的IQueryable,请尝试在其上调用ToList(),以便select表达式不会被发送到数据库。
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies.ToList()
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), "<a href=\""+Url.Action("Edit",new{id=company.Id})+"\">"+company.Name.ToString()+"</a>" }
}).ToArray()
};
答案 1 :(得分:0)
它正在尝试将i
转换为SQL并失败。生成的查询接近
SELECT companies.Id,
'<a href=\"Edit/' + companies.Id + '\">' + companies.Name + '</a>'
FROM companies
基本上,您的字符串连接正在发送到您的SQL服务器。