datatable怎么转化为iqueryable
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了datatable怎么转化为iqueryable相关的知识,希望对你有一定的参考价值。
参考技术A 扩展方法:public static DataTable ConvertToDataTable(thisIEnumerable enumerable)
var dataTable = new DataTable();
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(T)))
dataTable.Columns.Add(pd.Name, pd.PropertyType);
foreach (T item in enumerable)
var Row = dataTable.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
Row[pd.Name] = pd.GetValue(item);
dataTable.Rows.Add(Row);
return dataTable;
传统写法:
DataTable table = new DataTable();
table.Columns.Add("UserID", typeof (long));
table.Columns.Add("UserName", typeof (string));
IEnumerable<T> database = //获取数据源;
foreach (T info in database)
DataRow row = dt.NewRow();
row["ID"] = database.ID;
row["Name"] = database.Name;
table.Rows.Add(row);
return table;
c# 多维数组交错数组(转化为DataTable)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace ConsoleApplication31 { class Program { public static Array ReturnArray() { string[,,] arr = new string[2, 3, 4]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { arr[i, j, k] = i + "," + j + "," + k; } } } return arr; } private static Array GetJCSZ() { //string[][][] arr = new string[2][][]; //arr[0] = new string[2][]; //arr[0][0] = new string[3];//3 //arr[0][1] = new string[4];//4 //arr[0][0][0] = "0,0,0"; //arr[0][0][1] = "0,0,1"; //arr[0][0][2] = "0,0,2"; //arr[1] = new string[2][]; //arr[1][0] = new string[3]; //arr[1][1] = new string[4]; //arr[1][0][0] = "1,0,0"; //arr[1][0][1] = "1,0,1"; //arr[1][0][2] = "1,0,2"; //return arr; string[] arr = new string[3]; arr[0] = "0"; arr[2] = "2"; return arr; } public static void InitColumns(Array arr, ref Dictionary<string, DataColumn> dicCols, ref DataTable table) { for (int i = 0; i < arr.Length; i++) { if ((arr as dynamic)[i] is Array) { InitColumns((arr as dynamic)[i], ref dicCols, ref table); } else { if (arr.Length >= dicCols.Keys.Count) { dicCols.Clear(); for (int ii = 0; ii < arr.Length; ii++) { string colName = Guid.NewGuid().ToString(); DataColumn col = new DataColumn(colName); if (!dicCols.ContainsKey(colName)) { dicCols.Add(colName, col); } } } } } } public static DataTable ArrayConvert2DataTable(Array arr) { DataTable tmpT = new DataTable(); Dictionary<string, DataColumn> dicCols = new Dictionary<string, DataColumn>(); Dictionary<string, DataRow> dicRows = new Dictionary<string, DataRow>(); //J=交 C=错 bool isJC = !(arr.GetType().Name.Contains(‘,‘)); //交错数组处理 if (isJC) { //交错数组第一个维度的元素个 DataTable table = new DataTable(); List<int> dims = new List<int>(); InitColumns(arr, ref dicCols, ref table); foreach (var item in dicCols) { table.Columns.Add(item.Value); } int currRowIndex = 0; SearchTable(ref currRowIndex,arr,arr, ref table); return table; } //多维数组处理 else { int rank = arr.Rank; int cols = arr.GetLength(rank - 1); for (int i = 0; i < cols; i++) { DataColumn col = new DataColumn(Guid.NewGuid().ToString()); tmpT.Columns.Add(col); } Dictionary<int, int> dims = new Dictionary<int, int>(); int currRowIndex = -1; Dictionary<int, DataRow> dicRow = new Dictionary<int, DataRow>(); var iterator = arr.GetEnumerator(); int count = 0; while (iterator.MoveNext()) { var curr = iterator.Current; if (count % cols == 0) { currRowIndex++; DataRow dr = tmpT.NewRow(); tmpT.Rows.Add(dr); dicRow.Add(currRowIndex, dr); dr[0] = curr.ToString(); if (count == cols) { count = 0; } } else { tmpT.Rows[currRowIndex][count] = curr.ToString(); } count++; } } return tmpT; } private static void SearchTable(ref int currRowIndex, Array ori, Array curr, ref DataTable table) { for (int i = 0; i < curr.Length; i++) { bool isa = (curr as dynamic)[i] is Array; if (isa) { SearchTable(ref currRowIndex, ori, (curr as dynamic)[i], ref table); } else { if (table.Rows.Count < currRowIndex + 1) { DataRow newRow = table.NewRow(); table.Rows.Add(newRow); } try { table.Rows[currRowIndex][i] = (curr as Array).GetValue(i); } catch (Exception) { ; } if (i == curr.Length - 1) currRowIndex++; } } } static void Main(string[] args) { var t1 = ArrayConvert2DataTable(ReturnArray()); var t2 = ArrayConvert2DataTable(GetJCSZ()); Console.ReadKey(); } } }
以上是关于datatable怎么转化为iqueryable的主要内容,如果未能解决你的问题,请参考以下文章
C#代码中从SQL中查询到的DataTable传到一个javascript 函数中,请问怎么实现,最好有实例,非常感谢!!!
怎样把 “二维”datatable 转化为dictionary-CSDN论坛