开发者

Not able to Returning an Anonymous type

I have following function with returning Anonymous type.

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

but it will gives me an following Error: Cannot implicitly convert type 'AnonymousType#1' to 'System.Linq.IQueryable'

I have confusion for define return type function in following line

public IQueryable<TabMasterViewModel> ...


If you want some sort of type here, you can create a class or structure called


public class TabMasterDataInfo
{
 public int Total {get;set;}
 public int Page {get;set;}
 public int Records {get;set;}
 public IEnumerable Rows {get;set;}
}

and then


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

and your method returns then type TabMasterDataInfo

public TabMasterDataInfo GetJsonTabMasterList(string OrderByColumn, string OrderType, int PageSize, int CurrentPage)

This is the rough idea anyways.. assuming you want a type return (which I would far prefer to 'object' -there is no guessing then and its strongly typed.


Following are solution:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜