score:17
var s = string.Join(", ", files.Select(file => Path.GetExtension(file))
.Distinct(StringComparer.InvariantCultureIgnoreCase).ToArray());
score:15
Here's how:
String s = String.Join(", ", (from extension in extensions select extension.ToUpper()).ToArray());
Note, I would probably not write this as one line, rather like this:
String s = String.Join(", ",
(from extension in extensions
select extension.ToUpper()).ToArray());
If you don't mind just going for 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 variant would be to just call ToUpper
on the final string instead:
String s = String.Join(", ", extensions.ToArray()).ToUpper();
And finally, in .NET 4.0, String.Join
finally supports IEnumerable<String>
directly, so this is possible:
String s = String.Join(", ", extensions).ToUpper();
Note that per your question, this might lead to duplicates nonetheless. Consider what would happen if your original list of filenames contained both "filename.txt"
and "filename.TXT"
, these would be counted as two distinct extensions.
The call to ToUpper
should be moved up before the call to Distinct
to fix this.
Instead of the original Linq expression + code, I would rewrite the whole thing to this:
String[] distinctExtensions = files
.Select(fileName => Path.GetExtension(fileName).ToUpper())
.Distinct()
.ToArray();
String distinctExtensionsAsString = String.Join(", ", distinctExtensions);
If you add the following utility method to your code library, you can simplify it further:
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());
}
}
and then your code can look like this:
String distinctExtensionsAsString = files
.Select(fileName => Path.GetExtension(fileName).ToUpper())
.Distinct()
.Join(", ");
score:0
How about this:
String output = String.Join(", ",(from file in files
let index = file.LastIndexOf('.') + 1
select file.Substring(index)).Distinct().ToArray<string>());
score:1
How about this...
public static string ConvertListToString(List<string> list)
{
return list.Aggregate((x, y) => x + ", " + y).ToUpper();
}
This does it in one line, and I've moved the "ToUpper" out onto the final string so it's only called once.
Clearly you could then throw away the method ConvertListToString and inline if you wanted.
Source: stackoverflow.com
Related Articles
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- How can I turn this 12-line method into a 1-line LINQ expression?
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime GetDate()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method ..., and this method cannot be translated into a store expression
- Additional information: LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method,and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Single ToSingle(System.String)' method, and this method cannot be translated into a store expression
- Is there a way to turn this small method into a lambda or linq statement?
- Converting linq select into model, LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method,and this method cannot be translated into a store expression
- Error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Turn this code into LINQ
- LINQ to Entities does not recognize the method Urls.MeaningfulURL, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Boolean HasFlag(System.Enum)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- convert this LINQ expression into Lambda
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- How to split string before Binding in repeater
- How work the method TrueForAll? C# LINQ
- Error on querying element using LINQ to XML
- Split list into sublist ID wise C#
- Any way to make this LINQ faster?
- How to perform an efficient Order By in this particular scenario?
- Factorio Style Power Line in Unity
- Counting Items from Comma Delimited Tuple
- Null Reference Exception in a Dynamic LINQ Expression
- How to bind to a specific item inside a parent Collection MVVM WPF
- LINQ Grouping: Is there a cleaner way to do this without a for loop
- Entity framework, compare complex types
- Linq to Entities where clause on navigation property
- Expression Tree Errors as IQueryable but works as IEnumerable
- Update property in object collection with linq
- Linq Custom Sorting
- What are the major differences between Data Access Application Block, NHibernate, ADO.NET Entity Framework and LINQ to SQL?
- C# LINQ Add to list every regex value group
- Replacing foreach loops with LINQ expressions
- Lambda selecting most recent based upon three fields