Getting Error with Dictionary

Getting error while grouping the keywords and getting count

@devasaiprasad_K

finalText should of vartype dictionary not string


Hi @devasaiprasad_K ,

Could you let us know what should be your Desired output format ?

Currently, the Expression outputs a Dictionary value but the variable assigned is a String type, and hence the error.

Hi @devasaiprasad_K

Since finalText is of DataType System.String you can give below way:

finalText = String.Join(Vbcrlf, ArrayString.GroupBy(function(x) x.ToUpper).ToDictionary(Function(x) x.Key, function(x) x.Count))

Regards

Ok… let me share my input data
“I rang the bell he rang the bell”

Now I need count of all words in the string, i need to display word and no of occurrences in the string

Hi @devasaiprasad_K

Try this

(String.Join(Environment.NewLine,Input.Split(" "c) _
    .GroupBy(Function(word) word) _
    .Select(Function(Group) $"{group.Key}: {group.Count()} occurrences")))
1 Like

Hi @devasaiprasad_K

According to your code this is the workflow:

Assign-> Input = "I rang the bell he rang the bell"

Assign-> arrInput = Input.Split(" "c)

Assign-> finalText = String.Join(Vbcrlf, arrInput.GroupBy(function(x) x.ToUpper).ToDictionary(Function(x) x.Key, function(x) x.Count))

finalText is of DataType System.String. arrInput is of DataType Array(System.String).


Sequence12.xaml (9.4 KB)

Regards

Let me see I have done samecode previously but not giving expected result… Will check now

1 Like

If I want to assign the code into a variable will assigning it to a string variable works?

Can you help me in understanding what is the logic to convert to Dictionary… I am trying to learn things… So pls help me in clarifying

Yes @devasaiprasad_K

It works!!

Hi @devasaiprasad_K

String.Join(Vbcrlf, arrInput.GroupBy(function(x) x.ToUpper).ToDictionary(Function(x) x.Key, function(x) x.Count))

arrInput: This represents an array or collection of strings.
.GroupBy(Function(x) x.ToUpper()): This groups the elements of arrInput by converting each string to uppercase (x.ToUpper()). This ensures that case differences are ignored when grouping the strings.
.ToDictionary(Function(x) x.Key, Function(x) x.Count): This converts the grouped elements into a dictionary where:

  • Function(x) x.Key: Represents the key of the dictionary, which is the uppercase version of the string (as we grouped by uppercase earlier).
  • Function(x) x.Count: Represents the value of the dictionary, which is the count of occurrences of each string in the original arrInput.
    String.Join(VbCrLf, ...): Finally, String.Join concatenates the key-value pairs of the dictionary into a single string, where each key-value pair is separated by a newline character (VbCrLf).

Regards

1 Like

Hi @devasaiprasad_K

Hope you understand the syntax written. If yes, please mark my post as solution to close the loop or if you have questions I’m happy to help.

Regards

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.