Class DataTableExtensions
DataTable 扩展方法
Inherited Members
Namespace: Lett.Extensions
Assembly: Lett.Extensions.dll
Syntax
public static class DataTableExtensions
Fields
| Improve this Doc View SourceSupportedDataTypes
DataTable 支持的数据类型
Declaration
public static readonly IReadOnlyList<Type> SupportedDataTypes
Field Value
Type | Description |
---|---|
IReadOnlyList<Type> |
Methods
| Improve this Doc View SourceColumnsEnumerable(DataTable)
获取DataColumn可枚举对象
Declaration
public static IEnumerable<DataColumn> ColumnsEnumerable(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
IEnumerable<DataColumn> |
Examples
var rs = dataTable.ColumnsEnumerable();
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
InvalidCastException |
FirstRow(DataTable)
获取首行
Declaration
public static DataRow FirstRow(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
DataRow |
Examples
dataTable.FirstRow();
Exceptions
Type | Condition |
---|---|
ArgumentException | 当前DataTable没有行 |
GetColumnDataType(DataTable, Int32)
获取Column的数据类型
Declaration
public static Type GetColumnDataType(this DataTable this, int index)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
Int32 | index | 索引 |
Returns
Type | Description |
---|---|
Type |
Examples
var columnType = dataTable.GetColumnDataType(-1);
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
GetColumnDataType(DataTable, String)
获取Column的数据类型
Declaration
public static Type GetColumnDataType(this DataTable this, string columnName)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
String | columnName | 列名 |
Returns
Type | Description |
---|---|
Type |
Examples
var columnType = dt.GetColumnDataType("columnName");
Exceptions
Type | Condition |
---|---|
ArgumentException | DataTable中不包含 |
HasRows(DataTable)
是否存在数据行
Declaration
public static bool HasRows(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
dataTable.HasRows();
|
Improve this Doc
View Source
LastRow(DataTable)
获取末行
Declaration
public static DataRow LastRow(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
DataRow |
Examples
dataTable.LastRow();
Exceptions
Type | Condition |
---|---|
ArgumentException | 当前DataTable没有行 |
RowsEnumerable(DataTable)
获取DataRow可枚举对象
Declaration
public static IEnumerable<DataRow> RowsEnumerable(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
IEnumerable<DataRow> |
Examples
var rs = dataTable.RowsEnumerable();
Exceptions
Type | Condition |
---|---|
ArgumentNullException | DataRow 为空 |
InvalidCastException |
ToDynamicObjects(DataTable)
转换为动态对象集合
值为 Value 转换为 Null
Declaration
public static IEnumerable<dynamic> ToDynamicObjects(this DataTable this)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
IEnumerable<Object> |
Examples
var dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
dt.Columns.Add("col2", typeof(DateTime));
dt.Columns.Add("col3", typeof(decimal));
dt.Columns.Add("col4", typeof(string));
dt.Columns.Add("col5", typeof(string));
dt.Rows.Add("strVal", new DateTime(2019, 4, 1), 100.23m, DBNull.Value, null);
dt.Rows.Add("strVal2", new DateTime(2019, 4, 2), 122.23m, DBNull.Value, null);
var rs = dt.ToDynamicObjects().ToList();
// rs[0].col1 == "strVal"
// rs[0].col2 == new DateTime(2019, 4, 1)
// rs[1].col3 == 122.23m
// rs[1].col4 is null
// rs[1].col5 is null
|
Improve this Doc
View Source
ToEntityList<T>(DataTable)
转换为实体列表
使用DataRow中的值,填充 目标类型 的 FieldInfo 与 PropertyInfo
匹配规则: FieldInfo.Name 或 PropertyInfo.Name 与 ColumnName 相同
Declaration
public static List<T> ToEntityList<T>(this DataTable this)
where T : class, new()
Parameters
Type | Name | Description |
---|---|---|
DataTable | this |
Returns
Type | Description |
---|---|
List<T> | 返回 List< |
Type Parameters
Name | Description |
---|---|
T | 目标类型 |
Examples
var rs = dataTable.ToEntityList<TestClass1>();
Exceptions
| Improve this Doc View SourceToEntityList<T>(DataTable, Func<DataRow, T, T>)
转换为实体列表
Declaration
public static List<T> ToEntityList<T>(this DataTable this, Func<DataRow, T, T> converter)
where T : class, new()
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
Func<DataRow, T, T> | converter | 参数 DataRow: 当前行 参数 |
Returns
Type | Description |
---|---|
List<T> | 返回 List< |
Type Parameters
Name | Description |
---|---|
T | 目标类型 |
Examples
var rs = dt.ToEntityList<TestClass1>((row, newObj) =>
{
newObj.PublicField = row.Cell<string>("PublicField");
return newObj;
});
|
Improve this Doc
View Source
Update<T>(DataTable, Func<DataRow, Boolean>, String, T)
更新
DBNull.Value
进行填充Declaration
public static void Update<T>(this DataTable this, Func<DataRow, bool> selector, string columnName, T value)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
Func<DataRow, Boolean> | selector | DataRow 选择器 |
String | columnName | 列名 |
T | value |
Type Parameters
Name | Description |
---|---|
T |
Examples
dataTable.Update(row=> row["FRowId"].toString().Equals("rowId"), "FName", "this is name");
// it like SQL: UPDATE [table] SET FName = 'this is name' WHERE FRowId = 'rowId'
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ArgumentException |
|
Update<T>(DataTable, Func<DataRow, Boolean>, String, Func<Int32, DataRow, T>)
更新
DBNull.Value
进行填充Declaration
public static void Update<T>(this DataTable this, Func<DataRow, bool> selector, string columnName, Func<int, DataRow, T> func)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
Func<DataRow, Boolean> | selector | DataRow 选择器 |
String | columnName | 列名 |
Func<Int32, DataRow, T> | func | 委托方法 DataRow: 当前 DataRow |
Type Parameters
Name | Description |
---|---|
T |
Examples
_testTable1.Update(row=>true, "FName", (i, row) => $"{i}_{row["FRowId"]}");
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ArgumentException |
|
Update<T>(DataTable, String, T)
更新
DBNull.Value
进行填充Declaration
public static void Update<T>(this DataTable this, string columnName, T value)
Parameters
Type | Name | Description |
---|---|---|
DataTable | this | |
String | columnName | 列名 |
T | value |
Type Parameters
Name | Description |
---|---|
T |
Examples
_testTable1.Update("FName", "a");
// it like SQL: UPDATE [table] set FName = 'a'
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ArgumentException |
|