c# – 如何将IEnumerable>转换为List?
发布时间:2020-12-16 00:23:15 所属栏目:百科 来源:网络整理
导读:我真的不明白这件事.我需要将以下结果转换为List private void generateKeywords_Click(object sender,RoutedEventArgs e){ string srText = new TextRange( txthtmlsource.Document.ContentStart,txthtmlsource.Document.ContentEnd).Text; Liststring lstS
我真的不明白这件事.我需要将以下结果转换为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("rn","")) .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 }; } } 下面这一行失败,因为我无法正常转换.我的意思是不是错误,但也不是字符串列表 lstShuffle = GetPermutations(lstShuffle).Select(pr => pr.ToString()).ToList(); 解决方法
对于任何IEnumerable< IEnumerable< T>>我们可以简单地调用SelectMany.
例: IEnumerable<IEnumerable<String>> lotsOStrings = new List<List<String>>(); IEnumerable<String> flattened = lotsOStrings.SelectMany(s => s); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |