@UserName123 , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.
Dictionary<string, List<Structure>> myDic = new Dictionary<string, List<Structure>>();
string[] tests = new string[] { "test1", "test2", "test3", "test4" };
foreach (string test in tests)
{
myDic.Add(test, new List<Structure> { });
}
myDic["test1"].Add(new Structure { name1 = "A" });
myDic["test1"].Add(new Structure { name1 = "B" });
myDic["test2"].Add(new Structure { name1 = "A" });
myDic["test2"].Add(new Structure { name1 = "B" });
myDic["test2"].Add(new Structure { name1 = "C" });
myDic["test3"].Add(new Structure { name1 = "C" });
myDic["test3"].Add(new Structure { name1 = "A" });
myDic["test4"].Add(new Structure { name1 = "D" });
List<List<string>> all=new List<List<string>>();
var result=myDic.Select(x => x.Value.Distinct());
foreach (KeyValuePair<string, List<Structure>> pair in myDic)
{
List<string> list = new List<string>();
foreach (Structure item in pair.Value)
{
string value = item.name1;
list.Add(value);
}
all.Add(list);
}
var comm= all.SelectMany(x => x).Distinct()
.Where(x => all.Select(y => (y.Contains(x) ? 1 : 0))
.Sum() >=3).ToList();
foreach (KeyValuePair<string, List<Structure>> pair in myDic)
{
string key = pair.Key;
Console.WriteLine("The output Key is: " + key);
if(comm.Count()==0)
{
Console.WriteLine("throw the exception");
}
else
{
if(pair.Value.Select(i=>i.name1).Intersect(comm).Any())
{
foreach (var item in comm)
{
Console.WriteLine("The output value is: " + item);
}
}
else
{
foreach (var item in pair.Value)
{
Console.WriteLine("The output value is: " + item.name1);
}
}
}
}
I tested the above three conditions, they all work well.
The final condition result:
Best regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.