如何在 MVC4 的 if 条件下检查具有值“”的 String[] 数组?
How to check String[] array having value "" in an if condition in MVC4?
可能是问题看起来很简单,也可能是解决方案也是 simple.But我已经尝试了很多,但无法做到这一点,
我有一个 string[] 数组,它有一个 value=""。我想在 if 条件
中检查它
if (del.counts==null && del.mrate==null)
{
///
}
我已经尝试过 IsNullorEmpty,equal..etc 但没有给我解决方案
如果您知道要检查的元素在数组中的哪个位置,则需要使用数组索引。所以在你的情况下是这样的:
if (string.IsNullOrEmpty(del.counts[0]))
{
// Code
}
如果您不知道元素在哪里,您将需要使用 Linq 的 Any(...)
扩展 Mehtod。
if (del.counts.Any(value => string.IsNullOrEmpty(value))
{
// Code
}
如果数组的任何元素是 NullOrEmpty
,Any(...)
将 return true
,如果不是
,则为 false
因此您可以在您的应用程序中像这样使用它:
if (del.counts == null && del.mrate == null)
{
// Your code to handle if 'del.counts' and 'del.mrate' are null
}
// We know 'del.counts' is not null, but one of the elements may be NullOrEmpty
else if (del.counts.Any(value => string.IsNullOrEmpty(value)))
{
// Your code to handle if one of the counts elements IsNullOrEmpty
}
您可能想要删除 else if
并根据您的上下文将其替换为 if
语句
可能是问题看起来很简单,也可能是解决方案也是 simple.But我已经尝试了很多,但无法做到这一点, 我有一个 string[] 数组,它有一个 value=""。我想在 if 条件
中检查它if (del.counts==null && del.mrate==null)
{
///
}
我已经尝试过 IsNullorEmpty,equal..etc 但没有给我解决方案
如果您知道要检查的元素在数组中的哪个位置,则需要使用数组索引。所以在你的情况下是这样的:
if (string.IsNullOrEmpty(del.counts[0]))
{
// Code
}
如果您不知道元素在哪里,您将需要使用 Linq 的 Any(...)
扩展 Mehtod。
if (del.counts.Any(value => string.IsNullOrEmpty(value))
{
// Code
}
如果数组的任何元素是 NullOrEmpty
,Any(...)
将 return true
,如果不是
因此您可以在您的应用程序中像这样使用它:
if (del.counts == null && del.mrate == null)
{
// Your code to handle if 'del.counts' and 'del.mrate' are null
}
// We know 'del.counts' is not null, but one of the elements may be NullOrEmpty
else if (del.counts.Any(value => string.IsNullOrEmpty(value)))
{
// Your code to handle if one of the counts elements IsNullOrEmpty
}
您可能想要删除 else if
并根据您的上下文将其替换为 if
语句