将 IEnumerable<string> 转换为字典
Converting IEnumerable<string> to Dictionary
将 bool distinct
添加到方法 Splitter 并检查 distinct is true
代码是否损坏后。现在的查询不是字典而是 IEnumerable<string>
,而是 Dictionary<string, int>
。怎么解决的?
这是错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.Dictionary'. An explicit conversion exists (are you missing a cast?)
代码:
private Dictionary<string, int> Splitter(string[] file, bool distinct)
{
var query = file
.SelectMany(i => File.ReadLines(i)
.SelectMany(line => line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries))
.AsParallel()
.Select(word => word.ToLower())
.Where(word => !StopWords.Contains(word))
.Where(word => !StopWordsPl.Contains(word))
.Where(word => !PopulatNetworkWords.Contains(word))
.Where(word => !word.All(char.IsDigit)));
if (distinct)
{
query = query.Distinct();
}
query.GroupBy(word => word)
.ToDictionary(g => g.Key, g => g.Count());
return query;
}
ToDictionary
return就是您所需要的。只是 return 而不是 returning query
.
return query.GroupBy(word => word)
.ToDictionary(g => g.Key, g => g.Count());
将 bool distinct
添加到方法 Splitter 并检查 distinct is true
代码是否损坏后。现在的查询不是字典而是 IEnumerable<string>
,而是 Dictionary<string, int>
。怎么解决的?
这是错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.Dictionary'. An explicit conversion exists (are you missing a cast?)
代码:
private Dictionary<string, int> Splitter(string[] file, bool distinct)
{
var query = file
.SelectMany(i => File.ReadLines(i)
.SelectMany(line => line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries))
.AsParallel()
.Select(word => word.ToLower())
.Where(word => !StopWords.Contains(word))
.Where(word => !StopWordsPl.Contains(word))
.Where(word => !PopulatNetworkWords.Contains(word))
.Where(word => !word.All(char.IsDigit)));
if (distinct)
{
query = query.Distinct();
}
query.GroupBy(word => word)
.ToDictionary(g => g.Key, g => g.Count());
return query;
}
ToDictionary
return就是您所需要的。只是 return 而不是 returning query
.
return query.GroupBy(word => word)
.ToDictionary(g => g.Key, g => g.Count());