Converting IEnumerable <string> to Dictionary

after adding bool distinct

to Splitter method and checking if the distinct is true

code is broken. Now a question, not a dictionary IEnumerable<string>

, but it should be Dictionary<string, int>

. How can this be solved?

This is mistake:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.Dictionary'. Explicit conversion exists (are you missing the listing?)

And the code:

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;
}

      

+3


source to share


1 answer


ToDictionary

returns what you need. Just return it instead of returning query

.



return query.GroupBy(word => word)
            .ToDictionary(g => g.Key, g => g.Count());

      

+12


source







All Articles