Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ将字符串集合缩减为一个分隔字符串?_C#_Linq - Fatal编程技术网

C# 如何使用LINQ将字符串集合缩减为一个分隔字符串?

C# 如何使用LINQ将字符串集合缩减为一个分隔字符串?,c#,linq,C#,Linq,我有一个字符串列表,我想将它们作为带有分号分隔符的字符串转储 IEnumerable foo=来自傻瓜列表中的f 其中f.property==“bar” 选择f.title; 我现在想输出以下内容: title1;title2;title3;title4 如何做到这一点?使用因为.NET2.0,string类提供了一种方便的方法。虽然它最初只在数组上运行,.NET4添加了IEnumerable重载 IEnumerable<string> foo = from f in fooLi

我有一个字符串列表,我想将它们作为带有分号分隔符的字符串转储

IEnumerable foo=来自傻瓜列表中的f
其中f.property==“bar”
选择f.title;
我现在想输出以下内容:

title1;title2;title3;title4

如何做到这一点?

使用因为.NET2.0,string类提供了一种方便的方法。虽然它最初只在数组上运行,.NET4添加了IEnumerable重载

IEnumerable<string> foo = from f in fooList
                          where f.property == "bar"
                          select f.title;

Console.WriteLine(string.Join(";", foo));
IEnumerable foo=来自傻瓜列表中的f
其中f.property==“bar”
选择f.title;
Console.WriteLine(string.Join(“;”,foo));

使用LINQ而不是字符串。加入,因为这是所要求的。虽然真正的
String.Join
可能更安全/更容易下注

IEnumerable<string> foo = from f in fooList
                      where f.property == "bar"
                      select f.title;
string join = foo.Aggregate((s, n) => s + ";" + n);
IEnumerable foo=来自傻瓜列表中的f
其中f.property==“bar”
选择f.title;
字符串join=foo.Aggregate((s,n)=>s+“;”+n);

可能重复不确定为什么需要使用linq来执行此操作…只需按照建议使用string.join。@Rig他可能希望有一个内置的reduce方法,因为大多数函数式语言都附带它。确实,聚合是LINQ的reduce实现。值得一提的是,这个特殊的重载(将一个
IEnumerable
作为参数)只添加到.NET 4+1中:第一,没有那么详细,但仍然在帖子中超过了它们。谢谢。有了一个快速的响应,你不可能总是很详细:)是的,但你可以在之后编辑它长达2分钟,甚至在它说“编辑”之前:)这正是我想要的:LINQ版本的reduce。
IEnumerable<string> foo = from f in fooList
                      where f.property == "bar"
                      select f.title;
string join = foo.Aggregate((s, n) => s + ";" + n);