写老派最有效的方法是什么:
StringBuilder sb = new StringBuilder();
if (strings.Count > 0)
{
foreach (string s in strings)
{
sb.Append(s + ", ");
}
sb.Remove(sb.Length - 2, 2);
}
return sb.ToString();
...在LINQ吗?
写老派最有效的方法是什么:
StringBuilder sb = new StringBuilder();
if (strings.Count > 0)
{
foreach (string s in strings)
{
sb.Append(s + ", ");
}
sb.Remove(sb.Length - 2, 2);
}
return sb.ToString();
...在LINQ吗?
当前回答
这个答案显示了问题中要求的LINQ(聚合)的使用情况,而不是用于日常使用。因为它没有使用StringBuilder,所以对于很长的序列,它的性能会很糟糕。对于常规代码,使用String。如另一个答案所示
像这样使用聚合查询:
string[] words = { "one", "two", "three" };
var res = words.Aggregate(
"", // start with empty string to handle empty list case.
(current, next) => current + ", " + next);
Console.WriteLine(res);
这个输出:
, one, two, three
聚合是一个接受值集合并返回标量值的函数。T-SQL中的例子包括min、max和sum。VB和c#都支持聚合。VB和c#都支持聚合作为扩展方法。使用点表示法,可以简单地调用IEnumerable对象的方法。
记住,聚合查询是立即执行的。
更多信息- MSDN:聚合查询
如果你真的想使用聚合,使用CodeMonkeyKing在注释中提出的StringBuilder的变体,这将是与常规字符串相同的代码。Join包括对大量对象的良好性能:
var res = words.Aggregate(
new StringBuilder(),
(current, next) => current.Append(current.Length == 0? "" : ", ").Append(next))
.ToString();
其他回答
我代码中的真实例子:
return selected.Select(query => query.Name).Aggregate((a, b) => a + ", " + b);
查询是一个具有Name属性的对象,该属性是一个字符串,我想要所选列表上所有查询的名称,用逗号分隔。
我之前写过一篇博客,我所做的正是你想要的:
http://ondevelopment.blogspot.com/2009/02/string-concatenation-made-easy.html
在博客文章中描述了如何实现工作在IEnumerable上的扩展方法,并命名为Concatenate,这将允许您编写如下内容:
var sequence = new string[] { "foo", "bar" };
string result = sequence.Concatenate();
或者更复杂的事情,比如:
var methodNames = typeof(IFoo).GetMethods().Select(x => x.Name);
string result = methodNames.Concatenate(", ");
下面是我在查看了其他答案和类似问题(即聚合和连接失败时没有0个元素)中解决的问题后确定的联合Join/Linq方法。
string结果=字符串。加入(”、“分裂。Select(s => s. name);
或者(如果s不是字符串)
string结果=字符串。加入(”、“分裂。Select(s => s. tostring ()));
简单的 易于阅读和理解 适用于通用元素 允许使用对象或对象属性 处理长度为0的元素 可以使用附加的Linq滤波 表现良好(至少在我的经验中) 不需要(手动)创建一个额外的对象(例如StringBuilder)来实现
当然,Join处理了有时会潜入其他方法(for, foreach)的恼人的最后一个逗号,这就是为什么我在第一时间寻找Linq解决方案。
我要稍微欺骗一下,给出一个新的答案,它似乎总结了这里所有最好的东西,而不是把它粘在注释里。
你可以这样一行:
List<string> strings = new List<string>() { "one", "two", "three" };
string concat = strings
.Aggregate(new StringBuilder("\a"),
(current, next) => current.Append(", ").Append(next))
.ToString()
.Replace("\a, ",string.Empty);
编辑:你要么先检查一个空的枚举对象,要么添加一个.Replace("\a",string.Empty);到表达式的末尾。我想我可能是太聪明了。
来自@a.friend的答案可能会稍微更好一些,我不确定Replace与Remove相比在底层做了什么。唯一的另一个警告是,如果你想连接以\a结尾的字符串,你会失去分隔符…我觉得这不太可能。如果是这样的话,你还有其他奇特的角色可以选择。
当我使用linq解析IIS日志文件时,我做了以下快速和肮脏的操作,它工作得非常好@ 100万行(15秒),尽管在尝试200万行时得到了内存溢出错误。
static void Main(string[] args)
{
Debug.WriteLine(DateTime.Now.ToString() + " entering main");
// USED THIS DOS COMMAND TO GET ALL THE DAILY FILES INTO A SINGLE FILE: copy *.log target.log
string[] lines = File.ReadAllLines(@"C:\Log File Analysis\12-8 E5.log");
Debug.WriteLine(lines.Count().ToString());
string[] a = lines.Where(x => !x.StartsWith("#Software:") &&
!x.StartsWith("#Version:") &&
!x.StartsWith("#Date:") &&
!x.StartsWith("#Fields:") &&
!x.Contains("_vti_") &&
!x.Contains("/c$") &&
!x.Contains("/favicon.ico") &&
!x.Contains("/ - 80")
).ToArray();
Debug.WriteLine(a.Count().ToString());
string[] b = a
.Select(l => l.Split(' '))
.Select(words => string.Join(",", words))
.ToArray()
;
System.IO.File.WriteAllLines(@"C:\Log File Analysis\12-8 E5.csv", b);
Debug.WriteLine(DateTime.Now.ToString() + " leaving main");
}
我使用linq的真正原因是我之前需要的Distinct():
string[] b = a
.Select(l => l.Split(' '))
.Where(l => l.Length > 11)
.Select(words => string.Format("{0},{1}",
words[6].ToUpper(), // virtual dir / service
words[10]) // client ip
).Distinct().ToArray()
;