Show / Hide Table of Contents

    Class DataTableExtensions

    DataTable 扩展方法

    Inheritance
    Object
    DataTableExtensions
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: Lett.Extensions
    Assembly: Lett.Extensions.dll
    Syntax
    public static class DataTableExtensions

    Fields

    | Improve this Doc View Source

    SupportedDataTypes

    DataTable 支持的数据类型

    参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.data.datacolumn.datatype?view=netcore-2.0
    Declaration
    public static readonly IReadOnlyList<Type> SupportedDataTypes
    Field Value
    Type Description
    IReadOnlyList<Type>

    Methods

    | Improve this Doc View Source

    ColumnsEnumerable(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

    DataColumn为空

    InvalidCastException
    | Improve this Doc View Source

    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没有行

    | Improve this Doc View Source

    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

    index 索引超出了数组范围

    | Improve this Doc View Source

    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中不包含columnName

    | Improve this Doc View Source

    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没有行

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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<T>

    Type Parameters
    Name Description
    T

    目标类型

    Examples
    var rs = dataTable.ToEntityList<TestClass1>();
    Exceptions
    Type Condition
    ArgumentNullException

    this.DataRow 为空

    InvalidCastException
    FieldAccessException
    TargetException
    ArgumentException
    MethodAccessException
    TargetInvocationException
    ArgumentNullException

    this.RowsEnumerable(DataTable) 为空

    | Improve this Doc View Source

    ToEntityList<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: 当前行

    参数 T: 目标新实例

    Returns
    Type Description
    List<T>

    返回 List<T>

    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

    columnName is null

    ArgumentNullException

    T is null

    ArgumentException

    columnName not exist

    | Improve this Doc View Source

    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

    委托方法

    int: index
    DataRow: 当前 DataRow
    Type Parameters
    Name Description
    T
    Examples
    _testTable1.Update(row=>true, "FName", (i, row) => $"{i}_{row["FRowId"]}");
    Exceptions
    Type Condition
    ArgumentNullException

    columnName is null

    ArgumentNullException

    T is null

    ArgumentException

    columnName not exist

    | Improve this Doc View Source

    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

    columnName is null

    ArgumentNullException

    T is null

    ArgumentException

    columnName not exist

    • Improve this Doc
    • View Source
    Back to top Copyright (c) 2019 viacooky. All rights reserved.