Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Tuple IEnumerable to List<string> in C#

Tags:

c#

ienumerable

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!

like image 743
MeanNoob Avatar asked Jan 01 '23 09:01

MeanNoob


2 Answers

Select is probably what you want

var list = new List<string>(GetWord.Select(x => x.Item1));
like image 98
TheGeneral Avatar answered Jan 12 '23 08:01

TheGeneral


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();
like image 20
Fabio Avatar answered Jan 12 '23 10:01

Fabio