简体   繁体   中英

More Efficient If\Else?

Is there possibly a more condensed/efficient way of writing out this if/else statement? I am having to do a check for null on every single field in this database (close to 200 fields) and the code is going to look quite messy by the end. :\\

if (dr["OLD_NUMBER"] != DBNull.Value)
{
    lblOldNumber.Text = dr["OLD_NUMBER"].ToString();
}
else
{
    lblOldNumber.Text = string.Empty;
}
// A bunch more with different lbls and columns

You can get rid of the if entirely.

DBNull.Value.ToString() returns an empty string.

You could do

lblOldNumber.Text = dr["OLD_NUMBER"] != DBNull.Value ? dr["OLD_NUMBER"].ToString() : string.Empty;

The above statement would require less lines and looks more readable to me.

lblOldNumber.Text = string.Empty;
if (dr["OLD_NUMBER"] != DBNull.Value)
{
    lblOldNumber.Text = dr["OLD_NUMBER"].ToString();
}

or you cam make function like

void ApplyValue(Label label,object value, string defaultValue){
     label.Text =defaultValue;
     if (value != DBNull.Value)
     {
         label.Text = value.ToString();
     }
}

and use next code

ApllyValue(lblOldNumber,dr["OLD_NUMBER"],string.Empty);

you could use the following:

lblOldNumber.Text = dr["OLD_NUMBER"] != DBNull.Value ? dr["OLD_NUMBER"].ToString() : string.Empty;

More information about the ?: operator instead of if, can be found here: (C# Reference)

As a rule of thumb when you see this sort of repetition, write a function to do your processing.

string FormatIt(object value) 
{
   return value.ToString(); // or whatever the logic is like
}

Then:

lblOldNumber.Text = FormatIt(dr["OLD_NUMBER"]);

So, if you had to modify your code to format money or smth like that, you have one place to change.

Something like this:

var labels = new Dictionary<string, YourLabelClass>
                         {
                             {"OLD_NUMBER", lblOldNumber},
                             //Add your 200 fields here 
                             {"ANOTHER_NUMBER", lblAnotherNumber},
                         };

        foreach (var label in labels)
        {
            label.Value.Text = dr[label.Key].ToString();
        }

查询数据库时,可以从数据集中删除空值。

var result = (context.MyTable.Where(c => c.OLD_NUMBER != null));

I'd probably write a method:

void TextOrNull(object item, Label lbl)
{
  lbl.Text = item != DBNull.Value ? item.ToString() : String.Empty;
}

And call it like:

TextOrNull(dr["OLD_NUMBER"], lblOldNumber);

I would make a method that has this in, something like:

private void setLabelText( IDataRecord dr, string columnName, Label label )
{
    label.Text = string.Empty
    if (dr[columnName] != DBNull.Value)
    {
        label.Text = dr[columnName].ToString();
    }
}

Then simply call it with the appropriate labels and record names, etc.

setText( dr, "OLD_NUMBER", lblOldNumber );

I haven't experimented with this much but there is an ItemArray on the DataRow class that will return an object array of the items in the DataRow . Might be able to set up a loop and test values that way rather than hardcoding the name of the key.

Again not sure if this will work as I don't have a test scenario set up on my PC, but like this?

foreach (var col in dataRow.ItemArray)
{
    if(DBNull.Value != col)
        lbl.Text = col.ToString()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM