I really don't understand this T thing yet. I need to convert below result to List
private void generateKeywords_Click(object sender, RoutedEventArgs e)
{
string srText = new TextRange(
txthtmlsource.Document.ContentStart,
txthtmlsource.Document.ContentEnd).Text;
List<string> lstShuffle = srText.Split(' ')
.Select(p => p.ToString().Trim().Replace("\r\n", ""))
.ToList<string>();
lstShuffle = GetPermutations(lstShuffle)
.Select(pr => pr.ToString())
.ToList();
}
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(
IEnumerable<T> items)
{
if (items.Count() > 1)
{
return items
.SelectMany(
item => GetPermutations(items.Where(i => !i.Equals(item))),
(item, permutation) => new[] { item }.Concat(permutation));
}
else
{
return new[] { items };
}
}
this line below fails because i am not able to convert properly. i mean not error but not string list either
lstShuffle = GetPermutations(lstShuffle).Select(pr => pr.ToString()).ToList();
For any IEnumerable<IEnumerable<T>>
we can simply call SelectMany
.
Example:
IEnumerable<IEnumerable<String>> lotsOStrings = new List<List<String>>();
IEnumerable<String> flattened = lotsOStrings.SelectMany(s => s);
Since lstShuffle
implements IEnumerable<string>
, you can mentally replace T
with string
: you're calling IEnumerable<IEnumerable<string>> GetPermutations(IEnumerable<string> items)
.
As Alexi says, SelectMany(x => x)
is the easiest way to flatten an IEnumerable<IEnumerable<T>>
into an IEnumerable<T>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With