1.测试数据量小的时候是没有问题的。当数据量大的时候会出现问题。
这是数据量小的时候的代码,
public JsonResult GetMultiGND(MultiGNDModels mgm)
{
try
{
DataTable dt = com.canmax.DAL.DataAnalyse.GetMultiGND(mgm);
string js = JsonConvert.SerializeObject(dt);
return Json(js);
return Json(dt);
}
catch (Exception ee)
{
var resultmodel = new ResultModel<object>();
resultmodel.ErrorCode = "1";
resultmodel.Message = ee.Message;
return Json(resultmodel);
}
}
数据量大的话,需要用下面的代码
public JsonResult GetMultiGND(MultiGNDModels mgm)
{
try
{
DataTable dt = com.canmax.DAL.DataAnalyse.GetMultiGND(mgm);
List<MultiGNDDataModels> ls =
DataTableExtend.ToDataList<MultiGNDDataModels>(dt);
var jsonResult = Json(ls, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = Int32.MaxValue;
return jsonResult;
}
catch (Exception ee)
{
var resultmodel = new ResultModel<object>();
resultmodel.ErrorCode = "1";
resultmodel.Message = ee.Message;
return Json(resultmodel);
}
}
我的程序还会初选循环引用的错误,则需要把datatable转换为List即可。
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace com.canmax.Extend
{
/// <summary>
/// DataTable扩展方法类
/// </summary>
public static class DataTableExtend
{
/// <summary>
/// DataTable转成List
/// </summary>
public static List<T> ToDataList<T>(this DataTable dt)
{
var list = new List<T>();
var plist = new List<PropertyInfo>(typeof(T).GetProperties());
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
foreach (DataRow item in dt.Rows)
{
T s = Activator.CreateInstance<T>();
for (int i = 0; i < dt.Columns.Count; i++)
{
PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
if (info != null)
{
try
{
if (!Convert.IsDBNull(item[i]))
{
object v = null;
if (info.PropertyType.ToString().Contains("System.Nullable"))
{
v = Convert.ChangeType(item[i], Nullable.GetUnderlyingType(info.PropertyType));
}
else
{
v = Convert.ChangeType(item[i], info.PropertyType);
}
info.SetValue(s, v, null);
}
}
catch (Exception ex)
{
throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message);
}
}
}
list.Add(s);
}
return list;
}
/// <summary>
/// DataTable转成实体对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static T ToDataEntity<T>(this DataTable dt)
{
T s = Activator.CreateInstance<T>();
if (dt == null || dt.Rows.Count == 0)
{
return default(T);
}
var plist = new List<PropertyInfo>(typeof(T).GetProperties());
for (int i = 0; i < dt.Columns.Count; i++)
{
PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
if (info != null)
{
try
{
if (!Convert.IsDBNull(dt.Rows[0][i]))
{
object v = null;
if (info.PropertyType.ToString().Contains("System.Nullable"))
{
v = Convert.ChangeType(dt.Rows[0][i], Nullable.GetUnderlyingType(info.PropertyType));
}
else
{
v = Convert.ChangeType(dt.Rows[0][i], info.PropertyType);
}
info.SetValue(s, v, null);
}
}
catch (Exception ex)
{
throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message);
}
}
}
return s;
}
/// <summary>
/// List转成DataTable
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="entities">实体集合</param>
public static DataTable ToDataTable<T>(List<T> entities)
{
if (entities == null || entities.Count == 0)
{
return null;
}
var result = CreateTable<T>();
FillData(result, entities);
return result;
}
/// <summary>
/// 创建表
/// </summary>
private static DataTable CreateTable<T>()
{
var result = new DataTable();
var type = typeof(T);
foreach (var property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
var propertyType = property.PropertyType;
if ((propertyType.IsGenericType) && (propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
propertyType = propertyType.GetGenericArguments()[0];
result.Columns.Add(property.Name, propertyType);
}
return result;
}
/// <summary>
/// 填充数据
/// </summary>
private static void FillData<T>(DataTable dt, IEnumerable<T> entities)
{
foreach (var entity in entities)
{
dt.Rows.Add(CreateRow(dt, entity));
}
}
/// <summary>
/// 创建行
/// </summary>
private static DataRow CreateRow<T>(DataTable dt, T entity)
{
DataRow row = dt.NewRow();
var type = typeof(T);
foreach (var property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
row[property.Name] = property.GetValue(entity) ?? DBNull.Value;
}
return row;
}
}
}