linq查询结果怎么转换成datatable
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq查询结果怎么转换成datatable相关的知识,希望对你有一定的参考价值。
public DataTable Getusermessage(int siteid, string userpwd) DataClassesDataContext db = new DataClassesDataContext(); DataTable dt = new DataTable(); var messagelist = from m in db.pop_g where m.g_siteid == siteid orderby m.g_createtime descending select m; return dt; //怎么将messagelist转换成datatable返回呢?谢谢
参考技术A 用泛型啊,返回你的实体类格式。如public List<你的实体类名> Getusermessage(int siteid, string userpwd)
DataClassesDataContext db = new DataClassesDataContext();
DataTable dt = new DataTable();
var messagelist = from m in db.pop_g
where m.g_siteid == siteid
orderby m.g_createtime descending
select new 你的实体类()
类字段1=m.字段1,类字段2=m.字段2
return messagelist.tolist();
如果一定要用table的话,就麻烦得多了。给你个网址吧
http://hi.baidu.com/woxxf/item/a535e05c1ca2c4cdd3e10c9c 参考技术B 用OleDataDbAdapter,直接将结果存储到DataSet中 参考技术C 定义成随机变量var的不能转换。 参考技术D return messagelist.ToTable(); 第5个回答 2012-11-15 不知道
C# 怎么将json 转换成 datatable
[
"hasChildNode":"1","nodeId":"200","nodeName":"医药","nodeType":"文件夹",
"hasChildNode":"1","nodeId":"234","nodeName":"财会","nodeType":"文件夹",
"hasChildNode":"1","nodeId":"235","nodeName":"建筑","nodeType":"文件夹", "hasChildNode":"1","nodeId":"239","nodeName":"其它","nodeType":"文件夹"
]
每个大括号相当于一行数据 冒号前字段相当于 列名,后为列值
怎么解出呢
1. 获取的DataTable 对象 转换为Json 字符串
2. Json 字符串 转换为 DataTable数据集合
#region DataTable 转换为Json字符串实例方法
/// <summary>
/// GetClassTypeJosn 的摘要说明
/// </summary>
public class GetClassTypeJosn : IHttpHandler
/// <summary>
/// 文件名:DataTable 和Json 字符串互转
/// 版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:2013-08-03
/// </summary>
//用法说明实例
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
HttpRequest req = context.Request;
string method = req["method"].ToStr().ToLower();
//获取合同明细列表 DataTable 转换为Json字符串
if (method == "txtdate")
string json = "";
BO.MakeContractMx bll = new MakeContractMx();
DataSet ds = bll.GetDataTable();
if (ds.Tables.Count > 0)
json =ToJson(ds.Tables[0]);
context.Response.Write(json);
return;
public bool IsReusable
get
return false;
#endregion
#region Json字符串转换为DataTable 实例方法
public DataTable JsonToDataTable(json)
DataTable dt= ToDataTable(json);
return dt;
#endregion
#region DataTable 转换为Json 字符串
/// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string ToJson(this DataTable dt)
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
arrayList.Add(dictionary); //ArrayList集合中添加键值
return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
#endregion
#region Json 字符串 转换为 DataTable数据集合
/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTable(this string json)
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
foreach (Dictionary<string, object> dictionary in arrayList)
if (dictionary.Keys.Count<string>() == 0)
result = dataTable;
return result;
if (dataTable.Columns.Count == 0)
foreach (string current in dictionary.Keys)
dataTable.Columns.Add(current, dictionary[current].GetType());
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
dataRow[current] = dictionary[current];
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
catch
result = dataTable;
return result;
#endregion
#region 转换为string字符串类型
/// <summary>
/// 转换为string字符串类型
/// </summary>
/// <param name="s">获取需要转换的值</param>
/// <param name="format">需要格式化的位数</param>
/// <returns>返回一个新的字符串</returns>
public static string ToStr(this object s, string format = "")
string result = "";
try
if (format == "")
result = s.ToString();
else
result = string.Format("0:" + format + "", s);
catch
return result;
#endregion 参考技术A /// <summary>
/// 将 Json 解析成 DateTable。
/// Json 数据格式如:
/// table:[column1:1,column2:2,column3:3,column1:1,column2:2,column3:3] /// </summary>
/// <param name="strJson">要解析的 Json 字符串</param>
/// <returns>返回 DateTable</returns>
public static DataTable JsonToDataTable(string strJson)
// 取出表名
var rg = new Regex(@"(?<=)[^:]+(?=:\\[)", RegexOptions.IgnoreCase);
string strName = rg.Match(strJson).Value;
DataTable tb = null;
// 去除表名
strJson = strJson.Substring(strJson.IndexOf("[") + 1);
strJson = strJson.Substring(0, strJson.IndexOf("]"));
// 获取数据
rg = new Regex(@"(?<=)[^]+(?=)");
MatchCollection mc = rg.Matches(strJson);
for (int i = 0; i < mc.Count; i++)
string strRow = mc[i].Value;
string[] strRows = strRow.Split(',');
// 创建表
if (tb == null)
tb = new DataTable();
tb.TableName = strName;
foreach (string str in strRows)
var dc = new DataColumn();
string[] strCell = str.Split(':');
dc.ColumnName = strCell[0].Replace("\\"", "");
tb.Columns.Add(dc);
tb.AcceptChanges();
// 增加内容
DataRow dr = tb.NewRow();
for (int j = 0; j < strRows.Length; j++)
dr[j] = strRows[j].Split(':')[1].Replace("\\"", "");
tb.Rows.Add(dr);
tb.AcceptChanges();
return tb;
引入命名空间 System.Text.RegularExpressions。
参考技术B 循环 jason 自己转吧以上是关于linq查询结果怎么转换成datatable的主要内容,如果未能解决你的问题,请参考以下文章
请问如何把DataGrid上的datasource转换成datatable或者dataset啊