C# If 语句,其值来自表中的字段

发布于 2024-12-01 09:35:53 字数 255 浏览 2 评论 0原文

如何编写此语句以从数据库或表中获取值并验证如果 Value = Yes 它将返回“Result = 10”部分。该字段称为“ApprovedStatus”,值将为“否”或“是”。

Visual Studio 告诉我:“当前上下文中不存在名称“是””

If (ApprovedStatus.Equals = Yes)
{
result = 10;
}

else
{
result = 1;
}

How do I write this statement to get a value back from the database or table and validate that if the Value = Yes it will return the "Result =10" part. The field is called "ApprovedStatus" the value will either be "No" or "Yes".

Visual Studio Tells me this: "The name 'Yes' does not exist in the current context"

If (ApprovedStatus.Equals = Yes)
{
result = 10;
}

else
{
result = 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谷夏 2024-12-08 09:35:53

如果是字符串,请尝试 if (ApprovedStatus == "Yes");如果是布尔值,请尝试 if (ApprovedStatus)

Try if (ApprovedStatus == "Yes") if it's a string, or if (ApprovedStatus) if it's a bool.

厌味 2024-12-08 09:35:53

如果 ApprovedStatus 的类型为 bool,请执行以下操作:

if (ApprovedStatus) 

如果是字符串,则执行此操作,

if(ApprovedStatus == "Yes") 

因为如果 ApprovedStatus = "yes",则这将等于 false

而是使用

if(StringComparer.OrdinalIgnoreCase.Equals(ApprovedStatus,"Yes"))
    result = 10;
else
   result = 1;

请注意如果您这样做

if (ApprovedStatus.ToString().ToUpper().Equals("YES"))

if( ApprovedStatus.Equals("whatever",StringComparison.OrdinalIgnoreCase))

如果 ApprovedStatus 为 null,则会抛出空引用异常。
...如果该值来自数据库,则这是可能的。

If ApprovedStatus is of type bool, do:

if (ApprovedStatus) 

Should it be string, do NOT do this

if(ApprovedStatus == "Yes") 

because this will equal false if ApprovedStatus = "yes"

Instead use

if(StringComparer.OrdinalIgnoreCase.Equals(ApprovedStatus,"Yes"))
    result = 10;
else
   result = 1;

Note that if you do

if (ApprovedStatus.ToString().ToUpper().Equals("YES"))

or

if( ApprovedStatus.Equals("whatever",StringComparison.OrdinalIgnoreCase))

it will throw a null reference exception if ApprovedStatus is null.
...which is possible to likely if the value comes from a database.

大姐,你呐 2024-12-08 09:35:53

鉴于可用信息有限,最好的猜测...(假设 ApprovedStatus 是一个字符串)

if(ApprovedStatus == "Yes") 
{ 
   result = 10; 
} 

else 
{ 
   result = 1; 
} 

if(ApprovedStatus.Equals("Yes")) 
{ 
   result = 10; 
} 

else 
{ 
   result = 1; 
} 

Best guess given the limited info available... (Assuming ApprovedStatus is a String)

if(ApprovedStatus == "Yes") 
{ 
   result = 10; 
} 

else 
{ 
   result = 1; 
} 

or

if(ApprovedStatus.Equals("Yes")) 
{ 
   result = 10; 
} 

else 
{ 
   result = 1; 
} 
白首有我共你 2024-12-08 09:35:53

使用 String.Compare —— 效率更高。

if(String.Compare(ApprovedStatus, "Yes", true)==0){
 result = 10;
} else {
 result = 1;
}

Use String.Compare -- it's more efficient.

if(String.Compare(ApprovedStatus, "Yes", true)==0){
 result = 10;
} else {
 result = 1;
}
幸福不弃 2024-12-08 09:35:53

C# 中的布尔值是 truefalse。您应该查阅基本的 C# 教程,但您的检查可能如下所示:

if (ApprovedStatus)
{
   result = 10;
}
else
{
   result = 1;
}

它可以写得更短,如下所示:

result = ApprovedStatus ? 10 : 1;

Boolean values in C# are true and false. You should consult a basic C# tutorial, but your check has probably to look like this:

if (ApprovedStatus)
{
   result = 10;
}
else
{
   result = 1;
}

It can be written shorter as:

result = ApprovedStatus ? 10 : 1;
握住你手 2024-12-08 09:35:53
if (ApprovedStatus.Equals("Yes"))  <-- Case Sensitive
{

}

或者

if (ApprovedStatus.ToString().ToUpper() == "YES")
{

}
if (ApprovedStatus.Equals("Yes"))  <-- Case Sensitive
{

}

or

if (ApprovedStatus.ToString().ToUpper() == "YES")
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文