Dictionary Values to Lower

S

shapper

Hello,

I have a Dictionary<String, Object> where I know, in this case, that
Object will be a string.

I would like to lower all string values using a lambda expression.

How can I do this?

Thank You,

Miguel
 
P

Peter Duniho

shapper said:
Hello,

I have a Dictionary<String, Object> where I know, in this case, that
Object will be a string.

I would like to lower all string values using a lambda expression.

How can I do this?

What is the point of using a lambda expression? That seems like an
ridiculously arbitrary requirement to me.

Why not just this:

Dictionary<string, object> dict = …;

KeyValuePair<string, object>[] rgkvp = dict.ToArray();

foreach (KeyValuePair<string, object> kvp in rgkvp)
{
dict[kvp.Key] = ((string)kvp.Value).ToLower();
}

?

What possible advantage could there be in making it more complicated
than that?

Note that the most obvious implementations that use a lambda expression
are likely to use the expression to generate an anonymous method, which
in turn would be used to create a delegate instance passed to some
enumerative method for processing the dictionary. I haven't
double-checked, but I'm pretty sure that simply wouldn't work, since you
aren't allowed to modify the dictionary while you're enumerating it.

Pete
 
S

shapper

shapper said:
I have a Dictionary<String, Object> where I know, in this case, that
Object will be a string.
I would like to lower all string values using a lambda expression.
How can I do this?

What is the point of using a lambda expression?  That seems like an
ridiculously arbitrary requirement to me.

Why not just this:

   Dictionary<string, object> dict = …;

   KeyValuePair<string, object>[] rgkvp = dict.ToArray();

   foreach (KeyValuePair<string, object> kvp in rgkvp)
   {
     dict[kvp.Key] = ((string)kvp.Value).ToLower();
   }

?

What possible advantage could there be in making it more complicated
than that?

Note that the most obvious implementations that use a lambda expression
are likely to use the expression to generate an anonymous method, which
in turn would be used to create a delegate instance passed to some
enumerative method for processing the dictionary.  I haven't
double-checked, but I'm pretty sure that simply wouldn't work, since you
aren't allowed to modify the dictionary while you're enumerating it.

Pete

Hi,

This works fine:

foreach (string key in routes.Keys.ToArray())
routes[key] = ((String)routes[key]).ToLower();

And I was trying to use this way:
routes = routes.ToDictionary(r => r.Key, r => (object)
((string)r.Value).ToLower());

But in fact my routes is not a Dictionary. Is a RouteValueDictionary
as follows:

Public Class RouteValueDictionary _
Implements IDictionary(Of String, Object), _
ICollection(Of KeyValuePair(Of String, Object)), IEnumerable(Of
KeyValuePair(Of String, Object)), _
IEnumerable

So I get the error:
Cannot implicitly convert type
'System.Collections.Generic.Dictionary<string,object>' to
'System.Web.Routing.RouteValueDictionary'

Does this has a solution?

Loop is ok to ... I was just wondering how to have this working with a
Lambda to.

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
This works fine:

foreach (string key in routes.Keys.ToArray())
routes[key] = ((String)routes[key]).ToLower();

It's less efficient, but yes…you can do it that way too.
And I was trying to use this way:
routes = routes.ToDictionary(r => r.Key, r => (object)
((string)r.Value).ToLower());

That's completely different from the original question you asked.
But in fact my routes is not a Dictionary. Is a RouteValueDictionary
as follows:

Public Class RouteValueDictionary _
Implements IDictionary(Of String, Object), _
ICollection(Of KeyValuePair(Of String, Object)), IEnumerable(Of
KeyValuePair(Of String, Object)), _
IEnumerable

So I get the error:
Cannot implicitly convert type
'System.Collections.Generic.Dictionary<string,object>' to
'System.Web.Routing.RouteValueDictionary'

Does this has a solution?

Not unless you add something to your own code to convert a Dictionary to
a RouteValueDictionary.
Loop is ok to ... I was just wondering how to have this working with a
Lambda to.

The System.Linq.Enumerable class has never heard of your custom
RouteValueDictionary. You're not going to find a single method in the
class that returns that specific type.

Frankly, I suspect that it's a design mistake for you to have a custom
dictionary type. Most likely, your code would actually be better if
you'd just use the .NET System.Collections.Generic.Dictionary<TKey,
TValue> type, and of course if you did that then the Enumerable class
would work fine for you.

But it's impossible for me to say for sure without knowing more about
the overall design. I can only state the suspicion based on past
experience.

Barring that, then hopefully your own RouteValueDictionary type is
implemented as a container of an instance of Dictionary<TKey, TValue>.
In that case, it should be trivial for you to add a constructor or other
initializer to the type that takes as input an actual Dictionary<TKey,
TValue> instance and uses that instance for the internal dictionary
storage. You could even make that underlying storage typed as
IDictionary<TKey, TValue> instead, and allow the initialization to occur
with any arbitrary implementation of IDictionary<TKey, TValue> (such as
another instance of RouteValueDictionary, or similar type.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top