Hacker News new | ask | show | jobs
by rayvega 5885 days 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
1 comments

that's the same as

var foo= new List<int> {1, 2, 3};

var bar = foo.Select(x => x*2);

Yep, 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>.

And 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).