Show / Hide Table of Contents

    Class DataRowExtensions

    DataRow 扩展方法

    Inheritance
    Object
    DataRowExtensions
    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 DataRowExtensions

    Methods

    | Improve this Doc View Source

    Cell<T>(DataRow, String)

    获取当前行中某个列的值

    Declaration
    public static T Cell<T>(this DataRow this, string columnName)
        where T : IConvertible
    Parameters
    Type Name Description
    DataRow this
    String columnName

    列名

    Returns
    Type Description
    T

    columnName不存在 或 转换失败,返回 default(T)

    Type Parameters
    Name Description
    T

    目标类型,约束为 IConvertible

    Examples
    var rs = dataRow.Cell<string>("columnName"); // if fail return default(string)
    | Improve this Doc View Source

    Cell<T>(DataRow, String, T)

    获取当前行中某个列的值

    Declaration
    public static T Cell<T>(this DataRow this, string columnName, T defaultValue)
        where T : IConvertible
    Parameters
    Type Name Description
    DataRow this
    String columnName

    列名

    T defaultValue

    默认值

    Returns
    Type Description
    T

    columnName不存在 或 转换失败,返回 defaultValue

    Type Parameters
    Name Description
    T

    目标类型,约束为 IConvertible

    Examples
    var rs = dataRow.Cell("columnName", "abc"); // if fail return "abc"
    | Improve this Doc View Source

    Cell<T>(DataRow, String, Func<T>)

    获取当前行中某个列的值

    Declaration
    public static T Cell<T>(this DataRow this, string columnName, Func<T> func)
        where T : IConvertible
    Parameters
    Type Name Description
    DataRow this
    String columnName

    列名

    Func<T> func
    Returns
    Type Description
    T

    columnName不存在 或 转换失败,返回 func

    Type Parameters
    Name Description
    T

    目标类型,约束为 IConvertible

    Examples
    var rs = dataRow.Cell<string>("columnName", () => "func return value"); 
    | Improve this Doc View Source

    HasColumn(DataRow, String)

    列是否存在

    Declaration
    public static bool HasColumn(this DataRow this, string columnName)
    Parameters
    Type Name Description
    DataRow this
    String columnName

    列名

    Returns
    Type Description
    Boolean
    Examples
    row.HasColumn("FRowId");
    Exceptions
    Type Condition
    ArgumentNullException

    columnName is null or empty

    ArgumentException
    | Improve this Doc View Source

    SetValue<T>(DataRow, String, T)

    设置值

    出现异常时,使用 DBNull.Value 进行填充

    Declaration
    public static void SetValue<T>(this DataRow this, string columnName, T value)
    Parameters
    Type Name Description
    DataRow this
    String columnName
    T value
    Type Parameters
    Name Description
    T
    Examples
    row.SetValue("FColumnName", "value");
    // if column "FColumnName" type is bool, row["FColumnName"] is DBNull.Value
    Exceptions
    Type Condition
    ArgumentNullException

    columnName is null

    ArgumentNullException

    T is null

    ArgumentException

    columnName not exist

    | Improve this Doc View Source

    SetValue<T>(DataRow, String, T, Boolean)

    设置值

    Declaration
    public static void SetValue<T>(this DataRow this, string columnName, T value, bool isDefaultDbNull)
    Parameters
    Type Name Description
    DataRow this
    String columnName

    列名

    T value

    值

    Boolean isDefaultDbNull

    赋值出现异常时,是否使用 DBNull.Value 进行填充

    Type Parameters
    Name Description
    T
    Examples
    row.SetValue("FColumnName", "value", true);
    // if column "FColumnName" type is bool, row["FColumnName"] is DBNull.Value
    Exceptions
    Type Condition
    ArgumentNullException

    columnName is null

    ArgumentNullException

    T is null

    ArgumentException

    columnName not exist

    | Improve this Doc View Source

    ToDynamicObject(DataRow)

    转换为动态对象集合

    值为 Value 转换为 Null

    Declaration
    public static dynamic ToDynamicObject(this DataRow this)
    Parameters
    Type Name Description
    DataRow this
    Returns
    Type Description
    Object

    如果Cell为 Value 则动态对象属性的值为 null

    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);
    var rs = dt.Rows[0].ToDynamicObject();
    
    // rs.col1 == "strVal"
    // rs.col2 == new DateTime(2019, 4, 1)
    // rs.col3 == 100.23m
    // rs.col4 is null
    // rs.col5 is null
    | Improve this Doc View Source

    ToEntity<T>(DataRow)

    转换为实体

    使用DataRow中的值,填充 目标类型 的 FieldInfo 与 PropertyInfo

    匹配规则: FieldInfo.Name 或 PropertyInfo.Name 与 ColumnName 相同

    Declaration
    public static T ToEntity<T>(this DataRow this)
        where T : class, new()
    Parameters
    Type Name Description
    DataRow this
    Returns
    Type Description
    T

    目标对象 T

    Type Parameters
    Name Description
    T

    目标类型

    Examples
    dataTable.Rows[0].ToEntity<TestClass1>(); 
    Exceptions
    Type Condition
    FieldAccessException
    TargetException
    ArgumentException
    MethodAccessException
    TargetInvocationException
    | Improve this Doc View Source

    ToEntity<T>(DataRow, Func<DataRow, T, T>)

    转换为实体

    Declaration
    public static T ToEntity<T>(this DataRow this, Func<DataRow, T, T> converter)
        where T : class, new()
    Parameters
    Type Name Description
    DataRow this
    Func<DataRow, T, T> converter

    参数 DataRow: 当前行

    参数 T: 目标新实例

    Returns
    Type Description
    T

    目标对象 T

    Type Parameters
    Name Description
    T

    目标类型

    Examples
    var rs = dt.Rows[0].ToEntity<TestClass1>((row, newClass) =>
    {
    newClass.Property1    = row.Cell<string>("Property1");
    newClass.PublicField1 = row.Cell<string>("PublicField1");
    return newClass;
    });
    • Improve this Doc
    • View Source
    Back to top Copyright (c) 2019 viacooky. All rights reserved.