[英]More Efficient If\Else?
Is there possibly a more condensed/efficient way of writing out this if/else statement? 是否可能有更简洁/有效的方式写出此if / else语句? 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. 我必须对此数据库中的每个字段(将近200个字段)进行null检查,并且最后代码看起来会很混乱。 :\\ :\\
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. 你可以摆脱的if
完全。
DBNull.Value.ToString()
returns an empty string. DBNull.Value.ToString()
返回一个空字符串。
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 或者你像这样的make功能
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) 有关?:运算符而不是if的更多信息,请参见:(C#参考)
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
. 我还没有这么多的尝试,但有一个ItemArray
上DataRow
类将返回该项目的一个对象数组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? 再次不确定是否可以使用,因为我的PC上没有设置测试方案,但是像这样吗?
foreach (var col in dataRow.ItemArray)
{
if(DBNull.Value != col)
lbl.Text = col.ToString()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.