mihai
mihai

Reputation: 2824

IEnumerable<string> to Dictionary<char, IEnumerable<string>>

I suppose that this question might partially duplicate other similar questions, but i'm having troubles with such a situation:

I want to extract from some string sentences

For example from

`string sentence = "We can store these chars in separate variables. We can also test against other string characters.";`

I want to build an IEnumerable words;

var separators = new[] {',', ' ', '.'};

IEnumerable<string> words = sentence.Split(separators, StringSplitOptions.RemoveEmptyEntries);

After that, go throught all these words and take firs character into a distinct ascending ordered collection of characters.

var firstChars = words.Select(x => x.ToCharArray().First()).OrderBy(x => x).Distinct();

After that, go through both collections and for each character in firstChars get all items from words which has the first character equal with current character and create a Dictionary<char, IEnumerable<string>> dictionary.

I'm doing this way:

var dictionary = (from k in firstChars
                  from v in words
                  where v.ToCharArray().First().Equals(k)
                  select new { k, v })
                  .ToDictionary(x => x);

and here is the problem: An item with the same key has already been added. Whis is because into that dictionary It is going to add an existing character.

I included a GroupBy extension into my query

var dictionary = (from k in firstChars
                  from v in words
                  where v.ToCharArray().First().Equals(k)
                  select new { k, v })
                  .GroupBy(x => x)
                  .ToDictionary(x => x);

The solution above gives makes all OK, but it gives me other type than I need.

enter image description here What I should do to get as result an Dictionary<char, IEnumerable<string>>dictionary but not Dictionary<IGouping<'a,'a>> ?

The result which I want is as in the bellow image: enter image description here But here I have to iterate with 2 foreach(s) which will Show me wat i want... I cannot understand well how this happens ...

Any suggestion and advice will be welcome. Thank you.

Upvotes: 2

Views: 507

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149078

You can do this:

var words = ...
var dictionary = words.GroupBy(w => w[0])
                      .ToDictionary(g => g.Key, g => g.AsEnumerable());

But for matter, why not use an ILookup?

var lookup = words.ToLookup(w => w[0]);

Upvotes: 2

vc 74
vc 74

Reputation: 38179

As the relation is one to many, you can use a lookup instead of a dictionary:

var lookup = words.ToLookup(word => word[0]);

loopkup['s'] -> store, separate... as an IEnumerable<string>

And if you want to display the key/values sorted by first char:

for (var sortedEntry in lookup.OrderBy(entry => entry.Key))
{
  Console.WriteLine(string.Format("First letter: {0}", sortedEntry.Key);
  foreach (string word in sortedEntry)
  {
    Console.WriteLine(word);
  }
}

Upvotes: 3

Related Questions