Mvc 3+ExtJsデータフォーマット変換(修正)

24900 ワード

Mvc 3が持参した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;

        }