博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINQ返回DataTable类型 list转dataset 转换为JSON对象
阅读量:5902 次
发布时间:2019-06-19

本文共 5439 字,大约阅读时间需要 18 分钟。

using System.Web.Script.Serialization;using System.Collections.Generic;using System.Reflection;using System.Data;using System;namespace CommonCode{    public class Common    {        ///          /// LINQ返回DataTable类型        ///          public static DataTable ToDataTable
(IEnumerable
varlist) { DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; if (varlist == null) return dtReturn; foreach (T rec in varlist) { if (oProps == null) { oProps = ((Type)rec.GetType()).GetProperties(); foreach (PropertyInfo pi in oProps) { Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); } } DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps) { dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null); } dtReturn.Rows.Add(dr); } return dtReturn; } ///
/// list转dataset /// public static DataSet ConvertToDataSet
(IList
list) { if (list == null || list.Count <= 0) return null; DataSet ds = new DataSet(); DataTable dt = new DataTable(typeof(T).Name); DataColumn column; DataRow row; System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (T t in list) { if (t == null) continue; row = dt.NewRow(); for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { System.Reflection.PropertyInfo pi = myPropertyInfo[i]; String name = pi.Name; if (dt.Columns[name] == null) { if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]") { column = new DataColumn(name, typeof(Int32)); dt.Columns.Add(column); //row[name] = pi.GetValue(t, new object[] {i});//PropertyInfo.GetValue(object,object[]) if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = System.DBNull.Value; } else if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.DateTime]") { column = new DataColumn(name, typeof(DateTime)); dt.Columns.Add(column); if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = System.DBNull.Value; } else { column = new DataColumn(name, pi.PropertyType); dt.Columns.Add(column); row[name] = pi.GetValue(t, null); } } } dt.Rows.Add(row); } ds.Tables.Add(dt); return ds; } public delegate object[] CreateRowDelegate
(T t); public static string Serialize(DataTable dt) { List
> list = new List
>(); foreach (DataRow dr in dt.Rows) { Dictionary
result = new Dictionary
(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } int count = 0; try { count = Convert.ToInt32(dt.TableName); } catch (System.Exception ex) { count = dt.Rows.Count; } string strReturn = ""; if (count == 0) { strReturn = "{\"totalCount\":0,\"data\":[]}"; } else { strReturn = ConventToJson(list, count); } return strReturn; } ///
/// 转换为JSON对象 /// public static string ConventToJson
(List
list, int count) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string strJson = serializer.Serialize(list); strJson = strJson.Substring(1); strJson = strJson.Insert(0, "{totalCount:" + count + ",data:["); strJson += "}"; return strJson; } ///
/// 不需要分页 /// public static string Serialize(DataTable dt, bool flag) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List
> list = new List
>(); foreach (DataRow dr in dt.Rows) { Dictionary
result = new Dictionary
(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } return serializer.Serialize(list); } }}

 

转载于:https://www.cnblogs.com/randyzhuwei/p/4058797.html

你可能感兴趣的文章
常用Mysql或者PostGresql或者Greenplum的语句总结。
查看>>
工控随笔_12_西门子_WinCC的VBS脚本_03_变量类型
查看>>
appium 报错
查看>>
phpquery中文手册
查看>>
微信nickname乱码(emoji)及mysql编码格式设置(utf8mb4)解决的过程
查看>>
【转】C++ 笔试面试题目
查看>>
同步和异步的区别
查看>>
在ASP.NET MVC控制器中获取链接中的路由数据
查看>>
使用ASP.NET Atlas SortBehavior实现客户端排序
查看>>
图像滤镜处理算法:灰度、黑白、底片、浮雕
查看>>
多线程一个错误的例子
查看>>
默认网关及route print
查看>>
Servlet如何处理一个请求?
查看>>
Linux Daily2
查看>>
使用Jquery+CSS如何创建流动导航菜单-Fluid Navigation
查看>>
Office文档出错的几种原因与解决方法
查看>>
【实验报告】实验二:DHCP基本实验
查看>>
气质的培养(哈佛管理世界)
查看>>
Can&#39;t get Kerberos realm
查看>>
正则表达式 学习笔记1.1
查看>>