How to include this 12-line method in a single-line LINQ expression?

How to replace ConvertListToString(extensions)with an elegant LINQ instruction?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestExtn2343
{
    class Program
    {
        public static void Main(string[] args)
        {
            string[] files = { "test.txt", "test2.txt", 
                               "test.as", "notes.doc", 
                               "data.xml", "test.xml", 
                               "test.html", "notes.txt", 
                               "test.xls" };

            List<string> extensions = (from file in files
                             let index = file.LastIndexOf('.') + 1
                             select file.Substring(index)).Distinct().ToList<string>();

            Console.WriteLine("The kinds of file extensions used are {0}.", ConvertListToString(extensions));
            Console.ReadLine();
        }

        public static string ConvertListToString(List<string> list) {
            StringBuilder sb = new StringBuilder();
            int count = 1;
            foreach (var listItem in list)
            {
                sb.Append(listItem.ToUpper());
                if (count != list.Count)
                    sb.Append(", ");
                count++;
            }
            return sb.ToString();
        }

    }
}
+3
source share
4 answers
var s = string.Join(", ", files.Select(file => Path.GetExtension(file))
    .Distinct(StringComparer.InvariantCultureIgnoreCase).ToArray());
+17
source

Here's how:

String s = String.Join(", ", (from extension in extensions select extension.ToUpper()).ToArray());

Note, I probably would not write this as a single line, rather like this:

String s = String.Join(", ",
    (from extension in extensions
     select extension.ToUpper()).ToArray());

If you don't mind just using the Linq extension methods directly, instead of the Linq query syntax, you can use this:

String s = String.Join(", ", extensions.Select(e => e.ToUpper()).ToArray());

Another option would be to simply call ToUpperin the final line:

String s = String.Join(", ", extensions.ToArray()).ToUpper();

Finally, in .NET 4.0 String.Join, it finally supports IEnumerable<String>directly, so this is possible:

String s = String.Join(", ", extensions).ToUpper();

, . , , "filename.txt", "filename.txt", .

ToUpper Distinct, .

Linq + code :

String[] distinctExtensions = files
    .Select(fileName => Path.GetExtension(fileName).ToUpper())
    .Distinct()
    .ToArray();
String distinctExtensionsAsString = String.Join(", ", distinctExtensions);

, :

public static class StringExtensions
{
    public static String Join(this IEnumerable<String> elements, String separator)
    {
        if (elements is String[])
            return String.Join(separator, (String[])elements);
        else
            return String.Join(separator, elements.ToArray());
    }
}

:

String distinctExtensionsAsString = files
    .Select(fileName => Path.GetExtension(fileName).ToUpper())
    .Distinct()
    .Join(", ");
+15

...

public static string ConvertListToString(List<string> list) 
{ 
    return list.Aggregate((x, y) => x + ", " + y).ToUpper();
}

, "ToUpper" , .

, ConvertListToString , .

+1

:

String output = String.Join(", ",(from file in files
  let index = file.LastIndexOf('.') + 1
  select file.Substring(index)).Distinct().ToArray<string>());
0

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


All Articles