Remco1250
Remco1250

Reputation: 85

Loop through multiple dictionaries

I have 6 dictionaries. I want to compare another dictionaries against each one of them and see what dictionaries contains what strings. Is it possible to do with a foreach loop?

static Dictionary<string, int> d = new Dictionary<string, int>();
static Dictionary<string, double> dNL = new Dictionary<string, double>();
static Dictionary<string, double> dDE = new Dictionary<string, double>();
static Dictionary<string, double> dFR = new Dictionary<string, double>();
static Dictionary<string, double> dSP = new Dictionary<string, double>();
static Dictionary<string, double> dEN = new Dictionary<string, double>();
static Dictionary<string, double> dIT = new Dictionary<string, double>();

foreach (var f in d)
{
    if (dNL.ContainsKey(f.Key))
    {
        //add to a numeric?
    }
    if (dDE.ContainsKey(f.Key))
    {
        //add to a numeric?
    }
}

something like this?

what I currently have (and not working like intended):

// need to find a better solution
foreach (var f in d)
{
    if (dNL.ContainsKey(f.Key))
    {
        dNLtotaal++;
    }
}
foreach (var f in d)
{
    if (dDE.ContainsKey(f.Key))
    {
        dDEtotaal++;
    }
}
foreach (var f in d)
{
    if (dFR.ContainsKey(f.Key))
    {
        dFRtotaal++;
    }
}
foreach (var f in d)
{
    if (dSP.ContainsKey(f.Key))
    {
        dSPtotaal++;
    }
}
foreach (var f in d)
{
    if (dEN.ContainsKey(f.Key))
    {
        dENtotaal++;
    }
}
foreach (var f in d)
{
    if (dIT.ContainsKey(f.Key))
    {
        dITtotaal++;
    }
}
// NEED A MUCH BETTER SOLUTION
List<int> totaleD = new List<int>();
totaleD.Add(dNLtotaal);
totaleD.Add(dDEtotaal);
totaleD.Add(dFRtotaal);
totaleD.Add(dSPtotaal);
totaleD.Add(dENtotaal);
totaleD.Add(dITtotaal);
int max = !totaleD.Any() ? -1 : totaleD.Select((value, index) => new { Value = value, Index = index }).Aggregate((a, b) => (a.Value > b.Value) ? a : b).Index;
var maxIndex = totaleD.IndexOf(totaleD.Max());
Console.WriteLine(maxIndex);

Upvotes: 0

Views: 2860

Answers (3)

Dmitry
Dmitry

Reputation: 344

Why not to have 1 Dictionary instead of 6? And keep there a pair [string, List[SomeObject]] where SomeObject is a class like

class SomeObject
{
    public Enum Type;//dNL, dDE etc
    public double Value;
}

Upvotes: 0

Rob
Rob

Reputation: 27357

You can do something like this:

var items = d.Keys;
var dictionaries = new[] { dNL, dDE, dFR, dSP, dEN, dIT };
var result = dictionaries.Select((d, index) =>
    new {
        Index = index,
        Matches = items.Count(i => d.ContainsKey(i))
    })
    .OrderByDescending(i => i.Matches)
    .Select(i => i.Index)
    .FirstOrDefault();

Which gives you the index of the dictionary with the most matches

Upvotes: 3

kashi_rock
kashi_rock

Reputation: 557

You could use lambda expressions to get the desired results. In following example, I tried to use two dictionaries:

int dNLtotaal = 0;
Dictionary<string, double> dNL = new Dictionary<string, double>();
Dictionary<string, double> dDE = new Dictionary<string, double>();

dNL.Keys.Where(k => dDE.ContainsKey(k)).ToList().ForEach(k => dNLtotaal++);

Hope it helps

Upvotes: 0

Related Questions