patterncsharpModerate
Checking if a column belongs to a provided list
Viewed 0 times
columncheckingprovidedlistbelongs
Problem
I've the following code, which checks if one column belongs to the list I provide. After that, it takes the value contained in it.
I've tried and it's 9 times faster than raising and handling an exception. But I don't find this code very readable.
For example, I don't need to define a variable
Can you see a better solution?
I've tried and it's 9 times faster than raising and handling an exception. But I don't find this code very readable.
For example, I don't need to define a variable
drsIDAgentClient. I've thought of using an extension method over reader and provide a ColumnExist(string columnName) method.Can you see a better solution?
DataRow[] drsIDAgentClient = reader.GetSchemaTable().Select("ColumnName = 'IDAgentClient'");
if (drsIDAgentClient.Length > 0)
coupon.IdAgentClient = reader["IDAgentClient"] == DBNull.Value ? null : (int?)reader["IDAgentClient"];Solution
Can you see a better solution?
Yes use braces
Using
I also would enclose
After rereading your question it seems that you want to know if a column with a specific name exists in the
So you can use instead of the code above simply this
I would like to expand @Malachi's answer with not using a tenary
Malachi's check
can be rewritten like
and if you really need to use an extension method you can do this like
and call it
Yes use braces
{} also for single line statements of an if condition. Using
.Any() instead of checking .Length > 0 is faster but only for an IEnumerableI also would enclose
reader["IDAgentClient"] == DBNull.Value in () as it is more readable or even better you can introduce a boolean isDbNullValueDataRow[] drsIDAgentClient = reader.GetSchemaTable().Select("ColumnName = 'IDAgentClient'");
if (drsIDAgentClient.Length > 0)
{
Boolean isDbNullValue = (reader["IDAgentClient"] == DBNull.Value);
coupon.IdAgentClient = isDbNullValue ? null : (int?)reader["IDAgentClient"];
}After rereading your question it seems that you want to know if a column with a specific name exists in the
SchemaTable. So you can use instead of the code above simply this
String columnName = "IDAgentClient";
if (reader.GetSchemaTable().Columns.Contains(columnName))
{
Boolean isDbNullValue = (reader[columnName] == DBNull.Value);
coupon.IdAgentClient = isDbNullValue ? null : (int?)reader[columnName];
}I would like to expand @Malachi's answer with not using a tenary
Malachi's check
bool IsColumnEmpty(string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName))
? Reader[columnName] == DBNull.Value
: true;
}can be rewritten like
bool ColumnHasValue(string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName) &&
Reader[columnName] != DBNull.Value;
}and if you really need to use an extension method you can do this like
public static class HelperExtension
{
public static bool ColumnHasValue(this IDataReader reader, string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName) &&
Reader[columnName] != DBNull.Value;
}
}and call it
coupon.IdAgentClient = reader.ColumnHasValue(columnName) ? (int?)reader[columnName] : null;Code Snippets
DataRow[] drsIDAgentClient = reader.GetSchemaTable().Select("ColumnName = 'IDAgentClient'");
if (drsIDAgentClient.Length > 0)
{
Boolean isDbNullValue = (reader["IDAgentClient"] == DBNull.Value);
coupon.IdAgentClient = isDbNullValue ? null : (int?)reader["IDAgentClient"];
}String columnName = "IDAgentClient";
if (reader.GetSchemaTable().Columns.Contains(columnName))
{
Boolean isDbNullValue = (reader[columnName] == DBNull.Value);
coupon.IdAgentClient = isDbNullValue ? null : (int?)reader[columnName];
}bool IsColumnEmpty(string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName))
? Reader[columnName] == DBNull.Value
: true;
}bool ColumnHasValue(string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName) &&
Reader[columnName] != DBNull.Value;
}public static class HelperExtension
{
public static bool ColumnHasValue(this IDataReader reader, string columnName)
{
return reader.GetSchemaTable().Columns.Contains(columnName) &&
Reader[columnName] != DBNull.Value;
}
}Context
StackExchange Code Review Q#64263, answer score: 14
Revisions (0)
No revisions yet.