用Mvc3自带的Json()方法返回Json类型的数据有Bug,就是对带外键的表进行数据转换时报错“序列化类型为“System.Data.Entity.DynamicProxies. 。。。”的对象时检测到循环引用”
的错误,于是我就自己写了个解决了这个问题,可能会有一些小Bug,请大牛们指出!
1 /// <summary> 2 /// 绑定分页数据源格式转换 3 /// </summary> 4 /// <typeparam name="TEntity">实体</typeparam> 5 /// <param name="source">数据源</param> 6 /// <param name="flag">标记(“0”只显示List:'[{},{}]'"1"显示count,+List:{count:'',list:[{},{},{}]})</param> 7 /// <param name="pageindex">页索引</param> 8 /// <param name="pagesize">每页显示条数</param> 9 /// <returns>字符窜</returns> 10 public static String ConvertToJson<TEntity>(this IQueryable<TEntity> source, string flag, int? pageindex, int? pagesize) where TEntity : class 11 { 12 string sql = source.ToString(); 13 if (!source.ToList().Any()) 14 { 15 if (flag.Equals("0")) 16 { 17 return "[]"; 18 } 19 return "{count:0,list:[]}"; 20 } 21 StringBuilder builder = new StringBuilder(); 22 if (flag.Equals("1")) 23 { 24 builder.Append("{"); 25 builder.AppendFormat("count:{0},list:[", source.Count()); 26 } 27 if (flag.Equals("0")) 28 { 29 builder.Append("["); 30 } 31 IQueryable<TEntity> pagedSource = source.ToPagedList<TEntity>(pageindex, pagesize); 32 foreach (var obj in pagedSource) 33 { 34 builder.Append("{"); 35 PropertyInfo[] infos = obj.GetType().GetProperties(); 36 foreach (var item in infos) 37 { 38 builder.Append(item.Name); 39 builder.Append(":"); 40 if (item.PropertyType.IsGenericType && !item.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 41 { 42 var list = Funcs.ObjectToList(item.GetValue(obj, null)); 43 if (list != null) 44 { 45 builder.Append("["); 46 foreach (var child in list) 47 { 48 builder.Append("{"); 49 PropertyInfo[] childinfos = child.GetType().GetProperties(); 50 foreach (var chilitem in childinfos) 51 { 52 if (!chilitem.Name.Equals(typeof(TEntity).Name)) 53 { 54 builder.Append(chilitem.Name); 55 builder.Append(":"); 56 if (chilitem.GetValue(child, null) == null || chilitem.GetValue(child, null).Equals("")) 57 { 58 builder.Append("''"); 59 } 60 else 61 { 62 builder.Append("'" + chilitem.GetValue(child, null) + "'"); 63 } 64 builder.Append(","); 65 } 66 } 67 builder.Remove(builder.Length - 1, 1); 68 builder.Append("}"); 69 builder.Append(","); 70 } 71 builder.Remove(builder.Length - 1, 1); 72 builder.Append("]"); 73 } 74 else 75 { 76 builder.Append("''"); 77 } 78 } 79 else 80 { 81 if (item.GetValue(obj, null) == null || item.GetValue(obj, null).Equals("")) 82 { 83 builder.Append("''"); 84 } 85 else 86 { 87 builder.Append("'" + item.GetValue(obj, null) + "'"); 88 } 89 } 90 builder.Append(","); 91 } 92 builder.Remove(builder.Length - 1, 1); 93 builder.Append("}"); 94 builder.Append(","); 95 } 96 builder.Remove(builder.Length - 1, 1); 97 builder.Append("]"); 98 if (flag.Equals("1")) 99 { 100 builder.Append("}"); 101 } 102 103 return builder.ToString().Replace((char)13, (char)0).Replace((char)10, (char)0); 104 }
/// <summary> /// 分页 /// </summary> /// <typeparam name="TEntity">实体</typeparam> /// <param name="source">数据源</param> /// <param name="pageindex">页索引</param> /// <param name="pagesize">每页显示条数</param> /// <returns> </returns> public static IQueryable<TEntity> ToPagedList<TEntity>(this IQueryable<TEntity> source, int? pageindex, int? pagesize) { return source != null ? source.Skip(pagesize.Value * (pageindex.Value - 1)).Take(pageindex.Value * pagesize.Value) : null; }
/// <summary> /// 取得此对象的列表 /// </summary> /// <param name="target">数据源对象</param> /// <returns>object数组</returns> public static object[] ObjectToList(object target) { if (!Funcs.IsList(target)) return null; var count = Convert.ToInt32(target.GetType().GetProperty("Count").GetValue(target, null)); if (!count.Equals(0)) { object[] result = new object[count]; var enumObj = target as IEnumerable<object>; var i = 0; foreach (var item in enumObj) { result[i] = item; i++; } return result; } return null; }