久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查空值的最短方法,如果不是,則分配另一個

Shortest way to check for null and assign another value if not(檢查空值的最短方法,如果不是,則分配另一個值)
本文介紹了檢查空值的最短方法,如果不是,則分配另一個值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在從數據庫中提取 varchar 值并想設置 string 如果它們是 null 我將它們分配為">.我目前正在這樣做:

I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null. I'm currently doing it like this:

if (string.IsNullOrEmpty(planRec.approved_by) == true)
  this.approved_by = "";
else
  this.approved_by = planRec.approved_by.toString();

似乎應該有一種方法可以在一行中執行此操作,例如:

There seems like there should be a way to do this in a single line something like:

this.approved_by = "" || planRec.approved_by.toString();

但是我找不到最佳方法來做到這一點.有沒有更好的方法或者我有什么最好的方法來做到這一點?

However I can't find an optimal way to do this. Is there a better way or is what I have the best way to do it?

推薦答案

試試這個:

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? "" : planRec.approved_by.toString();

您也可以像其他人所說的那樣使用空合并運算符 - 因為沒有人給出適用于您的代碼的示例,這里是一個:

You can also use the null-coalescing operator as other have said - since no one has given an example that works with your code here is one:

this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();

但此示例僅適用于 this.approved_by 的可能值與您希望將其設置為的潛在值之一相同.對于所有其他情況,您將需要使用我在第一個示例中展示的條件運算符.

But this example only works since a possible value for this.approved_by is the same as one of the potential values that you wish to set it to. For all other cases you will need to use the conditional operator as I showed in my first example.

這篇關于檢查空值的最短方法,如果不是,則分配另一個值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
Overriding == operator. How to compare to null?(覆蓋 == 運算符.如何與空值進行比較?)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運算符)
C# null coalescing operator equivalent for c++(C# 空合并運算符等效于 C++)
主站蜘蛛池模板: 男女啪啪无遮挡 | 黄色三级视频在线观看 | 在线播放a | 在线观看av的网站 | 亚洲精品视频免费在线观看 | 日本不卡中文字幕 | 国产精品99久久久久久久久久久久 | 日本天堂网 | 国产日韩一区二区 | 黄色片播放| 精品国产一区二区在线观看 | 成人激情视频在线观看 | 天天草天天射 | 午夜私人影院 | 天堂网中文在线 | 青青草视频污 | 久久免费视频网站 | 日韩欧美综合 | 天天天天操 | 午夜视频在线 | 免费的黄色小视频 | 久久精品国产一区 | 中文字幕在线观看免费视频 | www.wuye | 欧美日韩国产在线观看 | av免费看片 | 老女人性生活视频 | 久久怡红院 | 久久av免费 | 快播少女爱欢乐 | 午夜大片| 中国毛片视频 | 97人人干| 国产欧美一区二区三区在线看蜜臀 | 国产一区二区不卡视频 | 成年人黄色 | 欧美一级黄色片 | 欧美日韩在线免费观看 | 国产成人三级 | 午夜视频一区二区 | 欧美在线视频播放 |