如何使用带单引号的 Linq 聚合

发布于 2024-12-15 06:53:31 字数 377 浏览 3 评论 0原文

假设我有一个字符串集合,如下所示:

IList<string> myList = new List<string>(){"one", "two", "three"};

使用 myList.Aggregate 我想以“'one', 'two', 'third'”(包括单引号)

结束有人有一种巧妙的方法来做到这一点,只使用聚合函数?我在想一些类似的事情

myList.Aggregate((increment, seed) => string.Concat(increment, ", ", seed));

,但这只是解决方案的一半。

Let's assume that I have a collection of strings, like so:

IList<string> myList = new List<string>(){"one", "two", "three"};

Using myList.Aggregate I would like to end up with "'one', 'two', 'three'" (including single quotes)

Does someone have a sleak way of doing this, only using the Aggregate function? I was thinking something in the lines of

myList.Aggregate((increment, seed) => string.Concat(increment, ", ", seed));

but that's only half the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-22 06:53:31

有什么理由使用Aggregate而不是更简单的string.Join

string joined = string.Join(", ", myCollection.Select(x => "'" + x + "'"));

(如果您使用的是 .NET 3.5,请添加 ToArray 调用。)

可以使用 Aggregate 实现 string.Join > (理想情况下使用StringBuilder),但这并不是很令人愉快。假设一个非空集合,我认为应该这样做:

string csv = myCollection.Aggregate(new StringBuilder("'"),
                 (sb, x) => sb.AppendFormat("{0}', '"),
                 sb => { sb.Length -= 3;
                         return sb.ToString(); });

Any reason to use Aggregate rather than the simpler string.Join?

string joined = string.Join(", ", myCollection.Select(x => "'" + x + "'"));

(Add a ToArray call if you're using .NET 3.5.)

You can implement string.Join using Aggregate (ideally using a StringBuilder) but it's not terribly pleasant. Assuming a non-empty collection, I think this should do it:

string csv = myCollection.Aggregate(new StringBuilder("'"),
                 (sb, x) => sb.AppendFormat("{0}', '"),
                 sb => { sb.Length -= 3;
                         return sb.ToString(); });
是你 2024-12-22 06:53:31

既然您指定“仅使用聚合函数”,那么:(

myCollection.Aggregate(string.Empty, (soFar, next) => 
                       soFar + (soFar == string.Empty ? soFar : ", ") 
                       + "'" + next + "'")

或)

myCollection.Aggregate(string.Empty, (soFar, next) =>
                                  string.Format("{0}{1}'{2}'", 
                                  soFar, soFar == string.Empty ? soFar : ", ", next))

以这种方式使用聚合来进行字符串连接,效率确实很低。

编辑:更新以删除前导 ,

Since you specify "only using the Aggregate function", how about:

myCollection.Aggregate(string.Empty, (soFar, next) => 
                       soFar + (soFar == string.Empty ? soFar : ", ") 
                       + "'" + next + "'")

(or)

myCollection.Aggregate(string.Empty, (soFar, next) =>
                                  string.Format("{0}{1}'{2}'", 
                                  soFar, soFar == string.Empty ? soFar : ", ", next))

Using Aggregate in this manner for string concatenation is really inefficient though.

EDIT: Updated to get rid of the leading ,.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文