前提需要替换查询得到的List当中的某个字段的值,替换规则有一个mapping关系
尝试代码 有问题 无法获取任何数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 获取Treegrid的List 3 /// </summary> 4 /// <returns></returns> 5 public JsonResult GetOrgnizationList() 6 { 7 IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization; 8 return Json(new treegrid 9 { 10 rows = list.Select( 11 s => new 12 { 13 _parentId = s.ParentCode, 14 OrganizationName = s.OrganizationName, 15 EnglishName = s.EnglishName, 16 OrganizationType = s.OrganizationType, 17 States = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("DicType",s.States), 18 OrganizationCode = s.OrganizationCode, 19 Order = s.Order 20 }).OrderBy(c => c.Order) 21 }, JsonRequestBehavior.AllowGet); 22 }
正常的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 获取Treegrid的List 3 /// </summary> 4 /// <returns></returns> 5 public JsonResult GetOrgnizationList() 6 { 7 IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization; 8 return Json(new treegrid 9 { 10 rows = list.Select( 11 s => new 12 { 13 _parentId = s.ParentCode, 14 OrganizationName = s.OrganizationName, 15 EnglishName = s.EnglishName, 16 OrganizationType = s.OrganizationType, 17 States = s.States, 18 OrganizationCode = s.OrganizationCode, 19 Order = s.Order 20 }).OrderBy(c => c.Order) 21 }, JsonRequestBehavior.AllowGet); 22 }
暂时未找到解决办法 如果有人知道怎么解决请留言 谢谢!
2013-08-05
解决办法:由于dynamic类型不能修改列表中的值(貌似是这样说的),所以解决办法是先将list给拿出来tolist(),然后foeach改变值,改完之后再去new row;代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public JsonResult GetOrgnizationList() { IQueryable<Rights_Sys_Organization> list = db.Rights_Sys_Organization; List<Rights_Sys_Organization> rlist = list.ToList(); foreach (var item in rlist) { item.States = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("OrgStates", item.States); } foreach (var item in rlist) { item.OrganizationType = SysDictionaryCollection.GetFieldTextByDicTypeAndValue("OrgType", item.OrganizationType); } dynamic rows = rlist.Select( s => new { _parentId = s.ParentCode, OrganizationName = s.OrganizationName, EnglishName = s.EnglishName, OrganizationType = s.OrganizationType, States = s.States, OrganizationCode = s.OrganizationCode, Order = s.Order }).OrderBy(c => c.Order); return Json(new treegrid { rows = rows }, JsonRequestBehavior.AllowGet); }