I need to array an ad-hoc set of strings like this
我需要像这样排列一组特殊的字符串
string a = null;
string b = "include me";
string c = string.Empty;
string d = "me too!";
without including null or empty strings. I know I can use a child function and params
:
不包括null或空字符串。我知道我可以使用子函数和params:
private List<string> GetUniqueKeys(params string[] list)
{
var newList = new List<string>();
foreach (string s in list)
{
if (!string.IsNullOrWhiteSpace(s))
newList.Add(s);
}
return newList;
}
///
return this.GetUniqueKeys(a, b, c, d).ToArray();
but is there any simpler way to do this that I'm not seeing?
但有没有更简单的方法来做到这一点,我没有看到?
EDIT: Sorry about that, happy to vote up the first LINQer, but I should have specified that I was trying to get rid of the child method altogether, not simplify it.
编辑:很抱歉,很高兴投票给第一个LINQer,但我应该指定我试图完全摆脱子方法,而不是简化它。
4 个解决方案
#1
3
No Child Function
没有儿童功能
The shortest way you can do this without a child function is the following:
没有子功能的最短方法是:
var a = new string[] { a, b, c, d }.Where(s => !string.IsNullOrWhiteSpace(s));
With Child Function
有儿童功能
However, I would recommend using your child function:
但是,我建议使用您的子功能:
private IEnumerable<string> GetUniqueKeys(params string[] list)
{
return list.Where(s => !string.IsNullOrWhitespace(s));
}
Extension Method
扩展方法
Alternatively, if you're really looking for other options... you could create an extension method:
或者,如果您真的在寻找其他选项......您可以创建一个扩展方法:
public static List<string> AddIfNotEmpty(this List<string> list, params string[] items)
{
list.AddRange(items.Where(s => !string.IsNullOrEmpty(s)));
return list;
}
Then use it like such:
然后像这样使用它:
var list = new List<string>().AddIfNotEmpty(a, b, c, d);
And add other items later:
并在以后添加其他项目:
list.AddIfNotEmpty("new item", string.Empty);
#2
4
If the input strings are enumerable, you can use linq.
如果输入字符串是可枚举的,则可以使用linq。
var result = stringList.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
#3
0
private List<string> GetUniqueKeys(params string[] list)
{
var newList = new List<string>();
newList.AddRange(list.Where(str => !string.IsNullOrEmpty(str)));
return newList;
}
#4
0
With Child method..
用Child方法..
private List<string> GetUniqueKeys(params string[] list)
{
return list.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
}
Without Child method..
没有儿童方法..
string a = null;
string b = "include me";
string c = string.Empty;
string d = "me too!";
string[] lst = { a, b, c, d };
var uniqueLst = lst.Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); //Or ToArray();
I recommend to use child method with params
.
我建议使用params的子方法。
#1
3
No Child Function
没有儿童功能
The shortest way you can do this without a child function is the following:
没有子功能的最短方法是:
var a = new string[] { a, b, c, d }.Where(s => !string.IsNullOrWhiteSpace(s));
With Child Function
有儿童功能
However, I would recommend using your child function:
但是,我建议使用您的子功能:
private IEnumerable<string> GetUniqueKeys(params string[] list)
{
return list.Where(s => !string.IsNullOrWhitespace(s));
}
Extension Method
扩展方法
Alternatively, if you're really looking for other options... you could create an extension method:
或者,如果您真的在寻找其他选项......您可以创建一个扩展方法:
public static List<string> AddIfNotEmpty(this List<string> list, params string[] items)
{
list.AddRange(items.Where(s => !string.IsNullOrEmpty(s)));
return list;
}
Then use it like such:
然后像这样使用它:
var list = new List<string>().AddIfNotEmpty(a, b, c, d);
And add other items later:
并在以后添加其他项目:
list.AddIfNotEmpty("new item", string.Empty);
#2
4
If the input strings are enumerable, you can use linq.
如果输入字符串是可枚举的,则可以使用linq。
var result = stringList.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
#3
0
private List<string> GetUniqueKeys(params string[] list)
{
var newList = new List<string>();
newList.AddRange(list.Where(str => !string.IsNullOrEmpty(str)));
return newList;
}
#4
0
With Child method..
用Child方法..
private List<string> GetUniqueKeys(params string[] list)
{
return list.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
}
Without Child method..
没有儿童方法..
string a = null;
string b = "include me";
string c = string.Empty;
string d = "me too!";
string[] lst = { a, b, c, d };
var uniqueLst = lst.Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); //Or ToArray();
I recommend to use child method with params
.
我建议使用params的子方法。