4 ms·
I personally like in simple scenarios using List<T>.ConvertAll: var foo = new List<int>{ 1, 2, 3}; var bar = foo.ConvertAll(x => x * 2); over the the
by rayvega 16y ago
I personally like in simple scenarios using List<T>.ConvertAll:
var foo = new List<int>{ 1, 2, 3};
var bar = foo.ConvertAll(x => x * 2);
over the the Linq equivalent:
var foo = new List<int>{ 1, 2, 3};
var bar = from x in foo select x * 2
- deleted 16y ago[deleted]
- WorkerBee 16y agothat's the same as var foo= new List<int> {1, 2, 3}; var bar = foo.Select(x => x*2);
- nlawalker 16y agoYep, one thing to realize is that LINQ does not simply refer to the query syntax - it refers to a set of technologies, including expression trees and the ability to compile them to alternate languages like TSQL, the query syntax, and the extension methods like Select. One small difference with your example though is that Select requires "using System.Linq", and ConvertAll is defined on List<T>.
- bluetech 16y agoAnd of course, the LINQ version returns an IEnumerable<TResult>, while the List version return List<TResult>. You could chain a .ToList() there to make them equivalent (if needed).