我有以下功能返回匿名类型。
public IQueryable<TabMasterViewModel> GetJsonTabMasterList(string OrderByColumn, string OrderType, int PageSize, int CurrentPage)
{
IQueryable<TabMaster> tabmasters = _tabmasterRepository.GetQueryable().OrderUsingSortExpression(OrderByColumn + " " + OrderType).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
var jsonData = new
{
total = 1,//totalPages,
page = 1,//page,
records = 14, //totalRecords,
rows = (from tm in tabmasters
select new
{
id = tm.colID,
cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName }
}).ToArray()
};
return jsonData;
}
但它会给我一个错误: 无法隐式转换类型'AnonymousType#1' 'System.Linq.IQueryable'
我对以下行中的定义返回类型函数感到困惑
public IQueryable<TabMasterViewModel> ...
答案 0 :(得分:1)
如果你想要某种类型,你可以创建一个名为
的类或结构
public class TabMasterDataInfo
{
public int Total {get;set;}
public int Page {get;set;}
public int Records {get;set;}
public IEnumerable Rows {get;set;}
}
然后
var jsonData = new TabMasterDataInfo()
{
Total = 1,//totalPages,
Page = 1,//page,
Records = 14, //totalRecords,
Rows = (from tm in tabmasters
select new
{
id = tm.colID,
cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName }
}).ToArray()
};
然后您的方法返回,然后键入TabMasterDataInfo
public TabMasterDataInfo GetJsonTabMasterList(string OrderByColumn, string OrderType, int PageSize, int CurrentPage)
这是一个粗略的想法反正..假设你想要一个类型返回(我更喜欢'对象' - 然后没有猜测它的强类型。
答案 1 :(得分:1)
以下是解决方案:
public JsonResult GetGridData(string sidx, string sord, int rows, int page)
{
int totalRecords = Convert.ToInt32(_tabmasterService.Count());
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(sidx, sord, rows, page);
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from tm in tabmasters
select new
{
id = tm.colID,
cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName }
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}