It's been a while since I've used lambda expressions or LINQ and am wondering how I would do the following (I know I can use a foreach loop, this is just out of curiosity) using both methods.
I have an array of string paths (does it make a difference if it's an array or list here?) from which I want to return a new list of just the filenames.
i.e. using a foreach loop it would be:
string[] paths = getPaths();
List<string> listToReturn = new List<string>();
foreach (string path in paths)
{
listToReturn.add(Path.GetFileName(path));
}
return listToReturn;
How would I do the same thing with both lambda and LINQ?
EDIT: In my case, I'm using the returned list as an ItemsSource for a ListBox (WPF) so I'm assuming it's going to need to be a list as opposed to an IEnumerable?
I think by "LINQ" you really mean "a query expression" but:
Note how the last one works by constructing the projection delegate from a method group, like this:
(Just in case that wasn't clear.)
Note that if you don't need to use this as a list - if you just want to iterate over it, in other words - you can drop the
ToList()
call from each of these approaches.You could do it like this:
It's just:
As already stated in other answers, if you don't actually need a
List<string>
you can omit theToList()
and simply returnIEnumerable<string>
(for example if you just need to iterate it,IEnumerable<>
is better because avoids the creation of an other list of strings)Also, given that
Select()
method takes a delegate, and there's an implicit conversion between method groups and delegates having the same signature, you can skip the lambda and just do:Your main tool would be the .Select() method.
No, an array also implements
IEnumerable<T>
Note that this minimal approach involves deferred execution, meaning that
fileNames
is anIEnumerable<string>
and only starts iterating over the source array when you get elements from it.If you want a List (to be safe), use
But when there are many files you might want to go the opposite direction (get the results interleaved, faster) by also using a deferred execution source:
It depends on what you want to do next with
fileNames
.