C# - IEnumerable 到分隔字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/167129/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:27:04  来源:igfitidea点击:

C# - IEnumerable to delimited string

c#functional-programming

提问by Jeremy

What is the functional programming approach to convert an IEnumerable<string>to a delimited string? I know I can use a loop, but I'm trying to wrap my head around functional programming.

将 an 转换IEnumerable<string>为分隔字符串的函数式编程方法是什么?我知道我可以使用循环,但我正在尝试围绕函数式编程。

Here's my example:

这是我的例子:

var selectedValues =
from ListItem item in checkboxList.Items
where item.Selected
select item.Value;

var delimitedString = ??

.. or could I do this in just the first var assignment (append each result to the previous)?

.. 或者我可以在第一个 var 赋值中做到这一点(将每个结果附加到前一个)?

采纳答案by Ilya Ryzhenkov

var delimitedString = selectedValues.Aggregate((x,y) => x + ", " + y);

回答by Tom Ritter

string.Join(", ", string[] enumerable)

回答by yfeldblum

var delimitedString = string.Join(",", checkboxList.Items.Where(i => i.Selected).Select(i => i.Value).ToArray());

回答by Marc Gravell

Well, in this case the functional approach might not be best suited, simply because there isn't a LINQ "ForEach", and you don't want to use string concatenation: you want to use StringBuilder. You could use ToArray (an example just appeared above), but I'd be tempted to simply use:

好吧,在这种情况下,函数式方法可能不是最适合的,仅仅是因为没有 LINQ“ForEach”,并且您不想使用字符串连接:您想使用 StringBuilder。您可以使用 ToArray (上面刚刚出现的示例),但我很想简单地使用:

    StringBuilder sb = new StringBuilder();
    foreach(ListViewItem item in checkboxList.SelectedItems) {
        if(sb.Length > 0) sb.Append(',');
        sb.Append(item.Text);
    }
    string s = sb.ToString();

Not functional programming, but it works... of course, if your source is alreadya string[] then string.Join is perfect. (LINQ is a great tool, but not necessarily always the best tool for every job)

不是函数式编程,但它有效……当然,如果您的源代码已经是 string[],那么 string.Join 是完美的。(LINQ 是一个很棒的工具,但不一定总是适合每项工作的最佳工具)

回答by JaredPar

Here's a LINQ/functional way of doing it.

这是执行此操作的 LINQ/功能方式。


string[] toDelimit = CallSomeFunction();
return toDelimit.Aggregate((x, y) => x + "," + y);

回答by Santiago Palladino

AviewAnew is the best answer, but if what you are looking for is learning how to think in functional, what you should do is use a fold operation (or aggregate as it is called in NET).

AviewAnew 是最好的答案,但是如果您正在寻找的是学习如何在函数式中思考,那么您应该做的是使用折叠操作(或在 NET 中称为聚合)。

items.Aggregate((accum, elem) => accum + ", " + elem);

回答by Danko Durbi?

Here's an example with a StringBuilder. The nice thing is that Append()returns the StringBuilderinstance itself.

这是一个带有StringBuilder. 好消息是Append()返回StringBuilder实例本身。

  return list.Aggregate( new StringBuilder(), 
                               ( sb, s ) => 
                               ( sb.Length == 0 ? sb : sb.Append( ',' ) ).Append( s ) );

回答by UNeverNo

This is 3.5 compatible:

这是 3.5 兼容的:

var selectedValues = String.Join(",", (from ListItem item in checkboxList.Items where item.Selected select item.Value).ToArray());