class Sample
{
static void Main(string[] args)
{
// create sequence
Item[] items = new Item[] { new Book{Id = 1, Price = 13.50, Genre = "Comedy", Author = "Jim Bob"},
new Book{Id = 2, Price = 8.50, Genre = "Drama", Author = "John Fox"},
new Movie{Id = 1, Price = 22.99, Genre = "Comedy", Director = "Phil Funk"},
new Movie{Id = 1, Price = 13.40, Genre = "Action", Director = "Eddie Jones"}};
var query1 = from i in items
where i.Price > 9.99
orderby i.Price
select i;
// load into new DataTable
DataTable table1 = query1.CopyToDataTable();
// load into existing DataTable - schemas match
DataTable table2 = new DataTable();
table2.Columns.Add("Price", typeof(int));
table2.Columns.Add("Genre", typeof(string));
var query2 = from i in items
where i.Price > 9.99
orderby i.Price
select new {i.Price, i.Genre};
query2.CopyToDataTable(table2, LoadOption.PreserveChanges);
// load into existing DataTable - expand schema + autogenerate new Id.
DataTable table3 = new DataTable();
DataColumn dc = table3.Columns.Add("NewId", typeof(int));
dc.AutoIncrement = true;
table3.Columns.Add("ExtraColumn", typeof(string));
var query3 = from i in items
where i.Price > 9.99
orderby i.Price
select new { i.Price, i.Genre };
query3.CopyToDataTable(table3, LoadOption.PreserveChanges);
// load sequence of scalars.
var query4 = from i in items
where i.Price > 9.99
orderby i.Price
select i.Price;
var DataTable4 = query4.CopyToDataTable();
}
public class Item
{
public int Id { get; set; }
public double Price { get; set; }
public string Genre { get; set; }
}
public class Book : Item
{
public string Author { get; set; }
}
public class Movie : Item
{
public string Director { get; set; }
}
}
public static class DataSetLinqOperators
{
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source)
{
return new ObjectShredder<T>().Shred(source, null, null);
}
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source,
DataTable table, LoadOption? options)
{
return new ObjectShredder<T>().Shred(source, table, options);
}
}
public class ObjectShredder<T>
{
private FieldInfo[] _fi;
private PropertyInfo[] _pi;
private Dictionary<string, int> _ordinalMap;
private Type _type;
public ObjectShredder()
{
_type = typeof(T);
_fi = _type.GetFields();
_pi = _type.GetProperties();
_ordinalMap = new Dictionary<string, int>();
}
public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options)
{
if (typeof(T).IsPrimitive)
{
return ShredPrimitive(source, table, options);
}
if (table == null)
{
table = new DataTable(typeof(T).Name);
}
// now see if need to extend datatable base on the type T + build ordinal map
table = ExtendTable(table, typeof(T));
table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator())
{
while (e.MoveNext())
{
if (options != null)
{
table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options);
}
else
{
table.LoadDataRow(ShredObject(table, e.Current), true);
}
}
}
table.EndLoadData();
return table;
}
public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options)
{
if (table == null)
{
table = new DataTable(typeof(T).Name);
}
if (!table.Columns.Contains("Value"))
{
table.Columns.Add("Value", typeof(T));
}
table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator())
{
Object[] values = new object[table.Columns.Count];
while (e.MoveNext())
{
values[table.Columns["Value"].Ordinal] = e.Current;
if (options != null)
{
table.LoadDataRow(values, (LoadOption)options);
}
else
{
table.LoadDataRow(values, true);
}
}
}
table.EndLoadData();
return table;
}
public DataTable ExtendTable(DataTable table, Type type)
{
// value is type derived from T, may need to extend table.
foreach (FieldInfo f in type.GetFields())
{
if (!_ordinalMap.ContainsKey(f.Name))
{
DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name]
: table.Columns.Add(f.Name, f.FieldType);
_ordinalMap.Add(f.Name, dc.Ordinal);
}
}
foreach (PropertyInfo p in type.GetProperties())
{
if (!_ordinalMap.ContainsKey(p.Name))
{
DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
: table.Columns.Add(p.Name, p.PropertyType);
_ordinalMap.Add(p.Name, dc.Ordinal);
}
}
return table;
}
public object[] ShredObject(DataTable table, T instance)
{
FieldInfo[] fi = _fi;
PropertyInfo[] pi = _pi;
if (instance.GetType() != typeof(T))
{
ExtendTable(table, instance.GetType());
fi = instance.GetType().GetFields();
pi = instance.GetType().GetProperties();
}
Object[] values = new object[table.Columns.Count];
foreach (FieldInfo f in fi)
{
values[_ordinalMap[f.Name]] = f.GetValue(instance);
}
foreach (PropertyInfo p in pi)
{
values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
}
return values;
}
}
轉 linqToDatatable
相關文章
- HString 轉 string 轉 HTuple
- 大轉轉FE招聘啦
- 玩轉SSH埠轉發
- 轉轉:2019年度轉轉二手交易服務白皮書
- Excel轉PDF怎麼轉?Excel轉PDF方法有哪些Excel
- 語音轉文字工具,語音轉文字怎樣轉?
- 轉
- 什麼是SSH埠轉發(本地轉發、遠端轉發、動態轉發)?
- 轉轉OLAP自助分析實踐
- 行轉列與列轉行
- SQL 行轉列,列轉行SQL
- Sting 轉List<String>轉List<Integer>
- (轉)OC專案轉Swift指南Swift
- 向上轉型和向下轉型
- Mysql - 行轉列、列轉行MySql
- heic格式轉換jpg工具——轉易俠heic轉換器
- ppt轉pdf怎麼轉?試試這個轉換技巧!
- iOS引用轉換:Foundation與Core Foundation物件互相轉換(__CFString轉NSString,void *轉id等等)iOS物件
- PDF轉PPT怎麼轉?好用的PDF轉換方法有哪些?
- heic格式轉換jpg免費怎麼轉?轉易俠可以
- 如何將圖片轉word?圖文轉換選轉易俠
- axmath 轉換latex 再轉 word公式公式
- c# svg轉Bitmap Bitmap轉ImageSourceC#SVG
- 影像縮放、旋轉、翻轉、平移
- heic格式批量轉jpg怎麼轉?
- pdf轉word如何線上轉換?
- JAVA字串轉日期或日期轉字串Java字串
- Oracle實驗(02):轉換 & 轉譯Oracle
- JS json字串轉物件、物件轉字串JSON字串物件
- PDF批次轉換器,批次轉word為pdf,批次轉ppt為pdf
- 轉發
- 轉跳
- [轉帖]
- RMAN(轉)
- python(轉)Python
- 可轉債轉股價值是什麼意思?可轉債轉股價值怎麼計算
- 容器和映象轉化、遷移方式【轉】
- 圖文轉換就選轉易俠