Search code examples
c#linqdictionarynested-loops

Traverse over a nested Dictionary using the nested dictionary as key in c# via Linq


so I have the following situation:

I have a Dictionary like this:

Dictionary<Dictionary<string, string>, DateTime> expireDates = new Dictionary<Dictionary<string, string>, DateTime>();

where the inner dictionary'first string is filled by a value (lets call it articlenumber) and the second string is filled by a value named articleCode. Together, they are the Key to get the preferred DateTime I need.

I try to get the DateTime via Linq like this in a WCF-Service:

var cmps= dbComponents.Select(component => new WCFObjects.Contracts.Component()
            {
                ArticleNumber = component.ArticleNumber,
                Sortiment = component.SortimentsCode,
                ... //irrelevant values
                //PSEUDOCODE, NOT WORKING
                 ExpireDate = expireDates.Where(x => x.Value.Where(y => x.Key.Where(
                    z => z.Key == component.ArticleNumber
                        && z.Value == component.SortimentsCode))).FirstOrDefault()

            }).ToList();

But I am struggling to create the preferred linq-query to get the value where key == articlenumber and value == sortimentscode. I tried a few approaches, most of them giving me the error

Error 'System.DateTime' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

but I can't figure it out and hope someone could help me out here.

Best regards, getoveritde


Solution

  • Try

    ExpireDate = expireDates.Where(
        x => x.Key.Any(
            y => y.Key == component.ArticleNumber && y.Value == component.SortimentsCode)
    ).Select(z => z.Value).FirstOrDefault()