Click here to Skip to main content
15,881,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Dictionary which contains string and structure in the below format


C#
Dictionary<string, List<Structure>> myDic = new Dictionary<string, List<Structure>>();

string[] tests = new string[] { "test", "test2", "test3", "test4" };

foreach (string test in tests)
{
    myDic.Add(value, new List<Structure>{ });
}

struct Structure
{
string name1;
string name2;

}


From the below code, I am printing 2 things
One is key (from list of "tests") and the other is the outputs that I have generated through different part of the code which is not mentioned here

for (int p=0; p< myDictionary.Keys.Count; p++)
{
    string key = myDictionary.ElementAt(p).Key;
    Console.WriteLine("The output Key is: " + key);

    for (int q = 0; q < myDictionary.ElementAt(p).Value.Count; q++)
    {
        string value = myDictionary.ElementAt(p).Value.ElementAt(q).name2;
        Console.WriteLine("The output value is: " + value);
    }
}


The output of the code goes as follows (I am iterating through count of "tests")
for iteration0:
The output key is: test1
The output value is: A

for iteration1:
The output key is: test2
The output value is: A
The output value is: B

for iteration2:
The output key is: test3
The output value is: A
The output value is: E
The output value is: F

for iteration3
The output key is: test4
The output value is: A


REQUIRED ALGORITHM:

Since A is present common in all iterations for test1, test2, test3 and test4
Output should be A for all the test1, test2 and test3, test4


If suppose iteration3 doesn't have A,
then output should be A for test1, test2 and test3 and test4 should return an exception

What I have tried:

I have tried few things but couldn't get it right
Posted
Updated 29-Mar-22 20:35pm
v7
Comments
Richard Deeming 29-Mar-22 12:18pm    
That's an odd way to iterate over a dictionary!
foreach (KeyValuePair<string, List<Structure>> pair in myDictionary)
{
    string key = pair.Key;
    Console.WriteLine("The output Key is: " + key);
    foreach (Structure item in pair.Value)
    {
        string value = item.name2;
        Console.WriteLine("The output value is: " + value);
    }
}


Beyond that, your explanation of the "required algorithm" is far from clear to me.
Maciej Los 29-Mar-22 14:56pm    
Sounds like an answer ;)
enoughIsenough 30-Mar-22 2:19am    
Hi @Richard, Thanks for making it simple, for iterating over a dictionary

From the comment that you've added for iterating over a dictionary, the output is as I have mentioned in my question.

THE OUTPUT GOES LIKE BELOW:
The key prints test1, test2, test3, test4 as outputs // (foreach KeyValuePair<string, list<structure="">> pair in myDictionary)

and the value prints
A for test1 iteration
A, B for test2 iteration
A, C, D for test3 iteration
A, C, F for test4 iteration

Now since A is common for all the tests (which is test1, test2, test3 and test4)

I need my output over as
"The output key is: test1, the output value is A"
"The output key is: test2, the output value is A"
"The output key is: test3, the output value is A"
"The output key is: test4, the output value is A"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

If there is no common value for all the Keys(test1, test2, test3 and test4), then I need to throw an exception

That is, if the
The Key is: test1
The value is: A

The Key is: test2
The value is: B

The Key is: test3
The value is: C

The Key is: test4
The value is: D

In this case since, there is no common value in for all the Keys, I need to throw an exception

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

If there is 3 common values for keys (i.e., as mentioned below)
The Key is: test1
The value is: A
The value is: B

The Key is: test2
The value is: A
The value is: B
The value is: C


The Key is: test3
The value is: C
The value is: A

The Key is: test4
The value is: D

In this case, the output needs to be
The key is: test1 and the value is: A
The key is: test2 and the value is: A
The key is: test3 and the value is: A
The key is: test4 and the value is: D
[no name] 1-Apr-22 11:30am    
Group the "values" by value so you wind up with a "distinct" list of values.

Then for each "test", compare the "distinct" list with the "test values". Any "distinct value" not in values for a give test is therefore "not a common value".

A couple of lines of LINQ.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900