MeanNoob
MeanNoob

Reputation: 41

Convert Tuple IEnumerable to List<string> in C#

I want to convert Tuple IEnumerable to a List<string>. For example, if there is

public static IEnumerable<Tuple<string, Type>> GetWord(String formula) 

how can I pass the enumerable directly into the constructor of a new List<string>???

Thanks!

Upvotes: 4

Views: 1456

Answers (2)

Fabio
Fabio

Reputation: 32445

With Select extension method you can convert enumerable of Tuple to the enumerable of string.
And then you can create a List<string> without explicitly passing enumerable to the List constructor, but by using ToList extension method.

var list = GetWord(formula).Select(t => t.Item1).ToList();

Upvotes: 2

TheGeneral
TheGeneral

Reputation: 81493

Select is probably what you want

var list = new List<string>(GetWord.Select(x => x.Item1));

Upvotes: 7

Related Questions