mercredi 28 janvier 2015

Is there a better way to get the column values from datatable


Vote count:

0




I am have to check if the column exists and then go ahead and get the value from the first row. But with so many ifs it looks quite longed



theData = GetData();
if (theData.Rows.Count > 0)
{
MyModel = new CustomModel();
dataSetRow = theData.Rows[0];
if (theData.Columns.Contains("Column1"))
{
if ((!object.ReferenceEquals(dataSetRow["Column1"], DBNull.Value)))
{
MyModel.Column1 = Convert.ToString(dataSetRow["Column1"]);
}
}
if (theData.Columns.Contains("Column2"))
{
if ((!object.ReferenceEquals(dataSetRow["Column2"], DBNull.Value)))
{
MyModel.Column2 = Convert.ToString(dataSetRow["Column2"]);
}
}
if (theData.Columns.Contains("Column3"))
{
if ((!object.ReferenceEquals(dataSetRow["Column3"], DBNull.Value)))
{
MyModel.Column3 = Convert.ToString(dataSetRow["Column3"]);
}
}
if (theData.Columns.Contains("Column4"))
{
if ((!object.ReferenceEquals(dataSetRow["Column4"], DBNull.Value)))
{
MyModel.Column4 = Convert.ToString(dataSetRow["Column4"]);
}
}


Is there a way I can make the code compact and not with so many ifs.



asked 30 secs ago

Simsons

2,617






Is there a better way to get the column values from datatable

Aucun commentaire:

Enregistrer un commentaire