Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how:</p> <pre><code>String s = String.Join(", ", (from extension in extensions select extension.ToUpper()).ToArray()); </code></pre> <p>Note, I would probably not write this as <em>one line</em>, rather like this:</p> <pre><code>String s = String.Join(", ", (from extension in extensions select extension.ToUpper()).ToArray()); </code></pre> <p>If you don't mind just going for the Linq extension methods directly, instead of the Linq query syntax, you can use this:</p> <pre><code>String s = String.Join(", ", extensions.Select(e =&gt; e.ToUpper()).ToArray()); </code></pre> <p>Another variant would be to just call <code>ToUpper</code> on the final string instead:</p> <pre><code>String s = String.Join(", ", extensions.ToArray()).ToUpper(); </code></pre> <p>And finally, in .NET 4.0, <code>String.Join</code> finally supports <code>IEnumerable&lt;String&gt;</code> directly, so this is possible:</p> <pre><code>String s = String.Join(", ", extensions).ToUpper(); </code></pre> <p>Note that per your question, this might lead to duplicates nonetheless. Consider what would happen if your original list of filenames contained both <code>"filename.txt"</code> and <code>"filename.TXT"</code>, these would be counted as two distinct extensions.</p> <p>The call to <code>ToUpper</code> should be moved up before the call to <code>Distinct</code> to fix this.</p> <p>Instead of the original Linq expression + code, I would rewrite the whole thing to this:</p> <pre><code>String[] distinctExtensions = files .Select(fileName =&gt; Path.GetExtension(fileName).ToUpper()) .Distinct() .ToArray(); String distinctExtensionsAsString = String.Join(", ", distinctExtensions); </code></pre> <p>If you add the following utility method to your code library, you can simplify it further:</p> <pre><code>public static class StringExtensions { public static String Join(this IEnumerable&lt;String&gt; elements, String separator) { if (elements is String[]) return String.Join(separator, (String[])elements); else return String.Join(separator, elements.ToArray()); } } </code></pre> <p>and then your code can look like this:</p> <pre><code>String distinctExtensionsAsString = files .Select(fileName =&gt; Path.GetExtension(fileName).ToUpper()) .Distinct() .Join(", "); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload