C# 检查列表字符串为 null 或为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19344754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check list string is null or empty
提问by user2869820
I have list with empty space("__")
我有空的列表(“ __”)
List<string> MyList = (List<string>)Session["MyList "];
if(MyList !=null || MyList != "")
{
}
MyList != "" does not work if string has more space so
如果字符串有更多空间,则 MyList != "" 不起作用
How can i check my list string is "" or null by using linq in c# ?
如何通过在 c# 中使用 linq 来检查我的列表字符串是 "" 还是 null?
采纳答案by Damith
if(MyList!=null || MyList.All(x=>string.IsNullOrWhiteSpace(x)))
{
}
回答by MichaC
var emptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList();
var listWithoutEmptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList();
If you just want to check if the list contains one ore more such items:
如果您只想检查列表是否包含一个或多个此类项目:
if (MyList.Any(p => string.IsNullOrWhiteSpace(p)))
{
}
If you want to check if all elements are null or empty
如果要检查所有元素是否为 null 或为空
if (MyList.All(p => string.IsNullOrWhiteSpace(p)))
{
}
回答by Hamlet Hakobyan
Try this:
尝试这个:
if(MyList.All(s=>string.IsNullOrWhiteSpace(s)))
{
....
}