score:17
var s = string.join(", ", files.select(file => path.getextension(file))
.distinct(stringcomparer.invariantcultureignorecase).toarray());
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.
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(", ");
Source: stackoverflow.com
Related Query
- How can I turn this 12-line method into a 1-line LINQ expression?
- How can I combine this code into one or two LINQ queries?
- How can I convert this linq expression to method form?
- How can i convert this code snippet into LINQ format?
- 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
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How can I use LINQ to project this parent and children object model into a flat, single object?
- 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 further simplify this piece of LINQ code
- How can I make this LINQ search method handle more than two terms?
- 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
- How can I turn a lambda expression specifying a property into an asp.net mvc compatible 'name' string representing the same?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Can this LINQ Expression be converted from method syntax to query syntax?
- How can I refactor this code for LINQ filtering?
- How can I code numerous MIN functions into one LINQ to DataSet query
- LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- How can I make a LINQ expression return into a class?
- LINQ to Entities does not recognize the method 'System.String ToString()' method and this method cannot be translated into a store expression
- How to convert Linq expression with multiple joins into method syntax OR retrieve select index?
- Can I can convert this C# code into some Linq code?
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- How can I convert Linq to Entities Query into expression tree?
- 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
More Query from same tag
- Convert Object[] to specific type
- Join and Include in Entity Framework
- Composite group join
- NHibernate.LINQ Supported Operators
- How do I get the values of all the nodes within the first and last child nodes of an element in C#
- Making 1 table join on 2 other tables with LINQ
- Concatanating a new object
- LINQ to find array indexes of a value
- Excel to SQL, C# libraries
- Strange T-SQL / LINQ Performance issue when paging
- LINQ and converting int? to int
- The right way to use lambda expressions in linq
- How to group on list of name with first letter?
- Performant way to fetch all rows inside and outside parent record
- Split comma separated string and compare each value in a List
- How to get the value of an expression
- How can I extract DbFunctions/SqlFunctions call into a resuable extension/method with EntityFramework 6.2?
- Creating a LINQ select from multiple tables
- Add xml child elements to specific parent node in C# with linq
- Skip and take method in List
- Pairing Collections in Linq without a For Loop
- LINQ with Querying "Memory"
- Using LINQ to get DataGridView row index where first column has specific value
- Linq: Totals Groups By Month
- Displaying equivalent to SQL pivot table using Linq against Entity Framework
- How to distinct for a list of list using LINQ?
- Is there a clever way to call a type dependent extension method dynamically?
- How does this RavenDB linq query work
- Compare DateTime Lists c# linq
- Calculation on List<> subset with Linq Skip and Take