How can I build a string from a collection with Linq?

I am creating flat file contents from line collections.

Collection example: A, B, C, D, E, etc.

I want to be able to output these values ​​to a line with linear channels in one fell swoop with Linq, if possible.

Output Example:

A

IN

FROM

D

E

and etc.

Here's the VB.NET code doing the current job:

For Each fieldValue As String In field.Values
   fileContent.Append(fieldValue + Environment.NewLine)
Next

I tried a bunch of ways to get Linq to do the job, but couldn't find the right combination. Thoughts?

+3
source share
8 answers

Have you tried a simple connection? ..

if field.Values ​​ , . LINQ .ToArray() .

string joined = string.Join(Environment.NewLine, field.Values);

VB

Dim joined As String = String.Join(Environment.NewLine, field.Values)

, , , LINQ, Aggregrate , .

field.Values.Aggregate(string.Empty, (s1, s2) => s1 += Environment.NewLine + s2);
+11

, . , , , , .

:

fileContent = field.Values.Contatenate(Environment.NewLine);

:

public static class EnumerableExtensions
{
    public static string Concatenate<T>(this IEnumerable<T> source, string seperator)
    {
        return Concatenate(source, i => i.ToString(), seperator);
    }

    public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string> selector, string seperator)
    {
        var builder = new StringBuilder();
        foreach (var item in source)
        {
            if (builder.Length > 0)
                builder.Append(seperator);

            builder.Append(selector(item));
        }
        return builder.ToString();
    }

    public static string ToCsv<T>(this IEnumerable<T> source)
    {
        return Concatenate(source, i => i.ToString(), ",");
    }

    public static string ToCsv<T>(this IEnumerable<T> source, Func<T, string> selector)
    {
        return Concatenate(source, selector, ",");
    }
}
+2

Aggregate, , , foreach, StringBuilder.

, , Linq - . Linq , . , , .

, foreach - Linq!

+1

StringBuilder Aggregate:

Dim fileContent As String = _
   field.Values.Aggregate( _
      New StringBuilder(), _
      Function(b, i) b.AppendLine(i) _
   ).ToString()
+1

, .NET 4.0 String.Concat <T> (), IEnumerable<T> ToString . !

+1

Values - , ...

field.Values.ForEach(o=>fileContent.Append(o + Environment.NewLine));
0

IEnumerable.Aggregate.

:

Dim fileContentString As String = _
        field.Values.Aggregate(Function(ByVal JoinedValues, ByVal DiscreteValue) 
            JoinedValues & Environment.NewLine & DiscreteValue)

- , Linq, , . , ForEach .

0

-

<Extension()> _
Public Function Flatten(ByVal stringList As IList(Of String), ByVal delimiter As String) As String
        Dim strB As New StringBuilder

        For Each Str As String In stringList
            If strB.ToString.Length > 0 Then
                strB.Append(delimiter)
            End If
            strB.Append(Str)
        Next
        Return strB.ToString
    End Function

, , , -

Dim letters as New List(Of String)
'do your loading of the list

Dim formattedString As String = letters.Flatten(Environment.NewLine)
0

Source: https://habr.com/ru/post/1719660/


All Articles