467,923 Members | 1,392 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Check valid particular Dictionary

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.
--
Luigi

Oct 2 '08 #1
  • viewed: 3544
Share:
6 Replies
Do you mean every string in every string[]? If so, do you have .NET 3.5
available to you? You could use:

IEnumerable<stringallValues = 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]
Oct 2 '08 #2
"Marc Gravell" wrote:
Do you mean every string in every string[]? If so, do you have .NET 3.5
available to you? You could use:

IEnumerable<stringallValues = 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
Oct 2 '08 #3
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
Oct 2 '08 #4
"Marc Gravell" wrote:
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
Have you something similar Marc?
The difficult for me is to have a Dictionary embedded in another dictionary.

L
Oct 2 '08 #5
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);
}
}
}
}
Oct 2 '08 #6
"Marc Gravell" wrote:
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
Oct 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Chris | last post: by
3 posts views Thread by Shapper | last post: by
4 posts views Thread by Joseph Bergevin | last post: by
5 posts views Thread by micklee74 | last post: by
3 posts views Thread by Peter | last post: by
16 posts views Thread by king kikapu | last post: by
22 posts views Thread by SmokeWilliams | last post: by
4 posts views Thread by sugard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.