我如何使用 linq 附加到每个项目的 stringbuilder

发布于 2024-10-30 23:59:03 字数 252 浏览 1 评论 0原文

我有一个 stringbuilder

Stringbuilder b = new StringBuilder();

,我想获取一个集合并使用 LINQ 附加集合中每个项目的属性名称。这可能吗?

像这样的东西:(

myCollection.Select(r=> b.Append(r.PropertyName);

但这似乎不起作用)

i have a stringbuilder

Stringbuilder b = new StringBuilder();

and i want to take a collection and append a propertyname from every item in the collection using LINQ. Is this possible.

something like this:

myCollection.Select(r=> b.Append(r.PropertyName);

(but this doesn't seem to work)

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

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

发布评论

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

评论(5

奶气 2024-11-06 23:59:03

这将起作用(尽管这是一种非常迂回的做事方式)并且适用于任何 IEnumerable

List<string> foo = new List<string>();
foo.Add("bar");
foo.Add("baz");
StringBuilder result = foo.Aggregate(new StringBuilder(), (a, b) => a.Append(b)); 

适应您的示例:

StringBuilder result = myCollection.Aggregate(new StringBuilder(), (a, b) => a.Append(b.PropertyName)); 

This would work (even though it's a very round-about way of doing things) and would work on any IEnumerable<string>:

List<string> foo = new List<string>();
foo.Add("bar");
foo.Add("baz");
StringBuilder result = foo.Aggregate(new StringBuilder(), (a, b) => a.Append(b)); 

Adapted to your example:

StringBuilder result = myCollection.Aggregate(new StringBuilder(), (a, b) => a.Append(b.PropertyName)); 
放手` 2024-11-06 23:59:03

当一个简单的 foreach 语句就足够具有表现力和简洁性时,您不需要在这里强制使用 LINQ 来解决这个问题。但是,如果您想使用更多功能式方法,您总是可以选择更像 string.Join 的东西,我相信它在内部使用 StringBuilder (但不引用我的话)。 但实际上

builder.Append(string.Join("", myCollection.Select(r => r.PropertyName)));

,在您可能看到的所有示例中,它们会比 更简洁(或更易读)吗

foreach (var item in myCollection) 
     builder.Append(item.PropertyName);

You don't need to force the issue here with using LINQ when a simple foreach statement will be plenty expressive and concise enough. However, if you want to use more functional-esque approach, you can always go for something more like string.Join which I believe uses a StringBuilder internally (but don't quote me on it). Such as

builder.Append(string.Join("", myCollection.Select(r => r.PropertyName)));

But really, of all the examples you might see, will they be any less verbose (or more readable) than

foreach (var item in myCollection) 
     builder.Append(item.PropertyName);

?

离线来电— 2024-11-06 23:59:03

我会将其写为:

String.Concat(myCollection.Select(r => r.PropertyName));

I would write it as:

String.Concat(myCollection.Select(r => r.PropertyName));

弥繁 2024-11-06 23:59:03

有些集合支持“Each”扩展方法:

myCollection.Each(r => b.Append(r.PropertyName));

编辑:再看一下,也许这不是开箱即用的。我正在考虑 List<> 上的“ForEach”方法班级。无论如何,您可以非常轻松地编写自己的 Each 方法:

public static void Each<T>(this IEnumerable<T> items, Action<T> action)
{
    if (items == null) return;

    var cached = items;

    foreach (var item in cached)
        action(item);
}

Some collections support an "Each" extension method:

myCollection.Each(r => b.Append(r.PropertyName));

EDIT: on second look, maybe this isn't provided out of the box. I was thinking of the "ForEach" method on the List<> class. Anyway, you can write your own Each method pretty easily:

public static void Each<T>(this IEnumerable<T> items, Action<T> action)
{
    if (items == null) return;

    var cached = items;

    foreach (var item in cached)
        action(item);
}
送君千里 2024-11-06 23:59:03
        List<string> words = new List<string>()
        { 
        "aaa",
        "aab",
        "aac",
        "aad",
        };

        StringBuilder sb = new StringBuilder();
        words.ForEach(a => sb.AppendFormat("Element {0}\n",a));
        List<string> words = new List<string>()
        { 
        "aaa",
        "aab",
        "aac",
        "aad",
        };

        StringBuilder sb = new StringBuilder();
        words.ForEach(a => sb.AppendFormat("Element {0}\n",a));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文