Check valid particular Dictionary

L

Luigi

Hi all,
I have to write a method that test a dictionary of this type:

Dictionary<string, Dictionary<string, string[]>>

to check if every string is not null or empty.

How can I write this?

Thanks in advance.
 
M

Marc Gravell

Do you mean every string in every string[]? If so, do you have .NET 3.5
available to you? You could use:

IEnumerable<string> allValues = outer.Values
.SelectMany(inner => inner.Values)
.SelectMany(array => array);

This yields all the string values from all the string[] arrays in turn.
If you just want to know if any are incorrect:

bool anyInvalid = outer.Values
.SelectMany(inner => inner.Values)
.SelectMany(array => array)
.Any(string.IsNullOrEmpty);

Marc
[C# MVP]
 
L

Luigi

Marc Gravell said:
Do you mean every string in every string[]? If so, do you have .NET 3.5
available to you? You could use:

IEnumerable<string> allValues = outer.Values
.SelectMany(inner => inner.Values)
.SelectMany(array => array);

This yields all the string values from all the string[] arrays in turn.
If you just want to know if any are incorrect:

bool anyInvalid = outer.Values
.SelectMany(inner => inner.Values)
.SelectMany(array => array)
.Any(string.IsNullOrEmpty);

Hi Mark,
sorry, but I have C# 2.0 (forget to write it first).

Luigi
 
M

Marc Gravell

sorry, but I have C# 2.0 (forget to write it first).

Then you'll just have to do a few foreach loops, I'm afraid...

Marc
 
L

Luigi

Marc Gravell said:
Then you'll just have to do a few foreach loops, I'm afraid...

Marc

Have you something similar Marc?
The difficult for me is to have a Dictionary embedded in another dictionary.

L
 
M

Marc Gravell

Something like below?

Dictionary<string, Dictionary<string, string[]>> outer =
new Dictionary<string, Dictionary<string, string[]>>();
foreach (KeyValuePair<string, Dictionary<string, string[]>>
outerPair in outer)
{
if (string.IsNullOrEmpty(outerPair.Key))
{
Console.WriteLine("outer key is null");
}
foreach (KeyValuePair<string, string[]> innerPair in
outerPair.Value)
{
if (string.IsNullOrEmpty(innerPair.Key))
{
Console.WriteLine("inner key is null; outer key: "
+ outerPair.Key);
}
foreach (string s in innerPair.Value)
{
if (string.IsNullOrEmpty(s))
{
Console.WriteLine("value is null; outer key: "
+ outerPair.Key
+ "; inner key: " + innerPair.Key);
}
}
}
}
 
L

Luigi

Marc Gravell said:
Something like below?

Dictionary<string, Dictionary<string, string[]>> outer =
new Dictionary<string, Dictionary<string, string[]>>();
foreach (KeyValuePair<string, Dictionary<string, string[]>>
outerPair in outer)
{
if (string.IsNullOrEmpty(outerPair.Key))
{
Console.WriteLine("outer key is null");
}
foreach (KeyValuePair<string, string[]> innerPair in
outerPair.Value)
{
if (string.IsNullOrEmpty(innerPair.Key))
{
Console.WriteLine("inner key is null; outer key: "
+ outerPair.Key);
}
foreach (string s in innerPair.Value)
{
if (string.IsNullOrEmpty(s))
{
Console.WriteLine("value is null; outer key: "
+ outerPair.Key
+ "; inner key: " + innerPair.Key);
}
}
}
}

Perfect!
Thank you so much Marc.

Luigi
 

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