Best practice to check if DataRow contains a certain column

后端 未结 5 2017
无人及你
无人及你 2020-12-13 17:28

At the moment, when I iterate over the DataRow instances, I do this.

foreach(DataRow row in table)
  return yield new Thingy { Name = row[\"hazaa\"]         


        
5条回答
  •  长情又很酷
    2020-12-13 17:47

    You can create an extension method to make it cleaner:

    static class DataRowExtensions
    {
        public static object GetValue(this DataRow row, string column)
        {
            return row.Table.Columns.Contains(column) ? row[column] : null;
        }
    }
    

    Now call it like below:

    foreach(DataRow row in table)
        return yield new Thingy { Name = row.GetValue("hazaa") };
    

提交回复
热议问题